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 FR_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 * Logging configuration for the server.
146 static const CONF_PARSER log_config[] = {
147 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
148 { "stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
150 { "file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
151 { "auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
152 { "auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
153 { "auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
154 { NULL, -1, 0, NULL, NULL }
159 * A mapping of configuration file names to internal variables
161 static const CONF_PARSER server_config[] = {
163 * FIXME: 'prefix' is the ONLY one which should be
164 * configured at compile time. Hard-coding it here is
165 * bad. It will be cleaned up once we clean up the
166 * hard-coded defines for the locations of the various
169 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
170 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
171 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
172 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
173 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
174 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &librad_dodns, "no" },
175 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
176 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
177 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
178 #ifdef DELETE_BLOCKED_REQUESTS
179 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
181 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &allow_core_dumps, "no" },
183 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
184 { "user", PW_TYPE_STRING_PTR, 0, &uid_name, NULL},
185 { "group", PW_TYPE_STRING_PTR, 0, &gid_name, NULL},
186 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
188 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
190 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
191 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
192 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
195 * People with old configs will have these. They are listed
196 * AFTER the "log" section, so if they exist in radiusd.conf,
197 * it will prefer "log_foo = bar" to "log { foo = bar }".
198 * They're listed with default values of NULL, so that if they
199 * DON'T exist in radiusd.conf, then the previously parsed
200 * values for "log { foo = bar}" will be used.
202 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, NULL },
203 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, NULL },
204 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, NULL },
205 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names, NULL },
206 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, NULL },
207 { NULL, -1, 0, NULL, NULL }
211 #define MAX_ARGV (256)
213 * Xlat for %{config:section.subsection.attribute}
215 static size_t xlat_config(void *instance, REQUEST *request,
216 char *fmt, char *out,
218 RADIUS_ESCAPE_STRING func)
224 request = request; /* -Wunused */
225 instance = instance; /* -Wunused */
228 * FIXME: radius_xlat, with a function that escapes
232 ci = cf_reference_item(request->root->config,
233 request->root->config, fmt);
234 if (!ci || !cf_item_is_pair(ci)) {
239 cp = cf_itemtopair(ci);
242 * Ensure that we only copy what's necessary.
244 * If 'outlen' is too small, then the output is chopped to fit.
246 value = cf_pair_value(cp);
248 if (outlen > strlen(value)) {
249 outlen = strlen(value) + 1;
253 return func(out, outlen, value);
258 * Xlat for %{client:foo}
260 static size_t xlat_client(UNUSED void *instance, REQUEST *request,
261 char *fmt, char *out,
263 UNUSED RADIUS_ESCAPE_STRING func)
265 const char *value = NULL;
268 if (!fmt || !out || (outlen < 1)) return 0;
270 if (!request || !request->client) {
275 cp = cf_pair_find(request->client->cs, fmt);
276 if (!cp || !(value = cf_pair_value(cp))) {
281 strlcpy(out, value, outlen);
287 * Recursively make directories.
289 static int r_mkdir(const char *part)
291 char *ptr, parentdir[500];
294 if (stat(part, &st) == 0)
297 ptr = strrchr(part, FR_DIR_SEP);
302 snprintf(parentdir, (ptr - part)+1, "%s", part);
304 if (r_mkdir(parentdir) != 0)
307 if (mkdir(part, 0770) != 0) {
308 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
316 * Checks if the log directory is writeable by a particular user.
318 static int radlogdir_iswritable(const char *effectiveuser)
321 struct passwd *pwent;
324 if (!radlog_dir || FR_DIR_IS_RELATIVE(radlog_dir))
327 if (r_mkdir(radlog_dir) != 0)
330 /* FIXME: do we have this function? */
331 if (strstr(radlog_dir, "radius") == NULL)
334 /* we have a logdir that mentions 'radius', so it's probably
335 * safe to chown the immediate directory to be owned by the normal
336 * process owner. we gotta do it before we give up root. -chad
339 if (!effectiveuser) {
344 pwent = getpwnam(effectiveuser);
346 if (pwent == NULL) /* uh oh! */
349 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
358 * Switch UID and GID to what is specified in the config file
360 static int switch_users(void)
362 int did_setuid = FALSE;
364 #ifdef HAVE_SYS_RESOURCE_H
365 struct rlimit core_limits;
370 if (gid_name != NULL) {
373 gr = getgrnam(gid_name);
375 if (errno == ENOMEM) {
376 radlog(L_ERR, "Cannot switch to Group %s: out of memory", gid_name);
378 radlog(L_ERR, "Cannot switch group; %s doesn't exist", gid_name);
382 server_gid = gr->gr_gid;
383 if (setgid(server_gid) < 0) {
384 radlog(L_ERR, "Failed setting Group to %s: %s",
385 gid_name, strerror(errno));
389 server_gid = getgid();
395 if (uid_name != NULL) {
398 pw = getpwnam(uid_name);
400 if (errno == ENOMEM) {
401 radlog(L_ERR, "Cannot switch to User %s: out of memory", uid_name);
403 radlog(L_ERR, "Cannot switch user; %s doesn't exist", uid_name);
407 server_uid = pw->pw_uid;
408 #ifdef HAVE_INITGROUPS
409 if (initgroups(uid_name, server_gid) < 0) {
410 if (errno != EPERM) {
411 radlog(L_ERR, "Failed setting supplementary groups for User %s: %s", uid_name, strerror(errno));
416 if (setuid(server_uid) < 0) {
417 radlog(L_ERR, "Failed setting User to %s: %s", uid_name, strerror(errno));
422 * Now core dumps are disabled on most secure systems.
428 #ifdef HAVE_SYS_RESOURCE_H
429 /* Get the current maximum for core files. */
430 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
431 radlog(L_ERR, "Failed to get current core limit: %s", strerror(errno));
437 * Core dumps are allowed if we're in debug mode, OR
438 * we've allowed them, OR we did a setuid (which turns
441 * Otherwise, disable core dumps for security.
444 if (!(debug_flag || allow_core_dumps || did_setuid)) {
445 #ifdef HAVE_SYS_RESOURCE_H
446 struct rlimit no_core;
448 no_core.rlim_cur = 0;
449 no_core.rlim_max = 0;
451 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
452 radlog(L_ERR, "Failed disabling core dumps: %s",
459 * Otherwise, re-enable core dumps if we're
460 * running as a daemon, AND core dumps are
461 * allowed, AND we changed UID's.
463 } else if ((debug_flag == 0) && allow_core_dumps && did_setuid) {
465 * Set the dumpable flag.
467 #ifdef HAVE_SYS_PRCTL_H
468 #ifdef PR_SET_DUMPABLE
469 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
470 radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
477 * Reset the core dump limits again, just to
478 * double check that they haven't changed.
480 #ifdef HAVE_SYS_RESOURCE_H
481 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
482 radlog(L_ERR, "Cannot update core dump limit: %s",
488 radlog(L_INFO, "Core dumps are enabled.");
491 * Else we're debugging (so core dumps are enabled)
492 * OR we're not debugging, AND "allow_core_dumps == FALSE",
493 * OR we're not debugging, AND core dumps are allowed,
494 * BUT we didn't call setuid, so we haven't changed the
495 * core dump capabilities inherited from the parent shell.
498 #if defined(HAVE_PWD_H) && defined(HAVE_GRP_H)
500 * We've probably written to the log file already as
501 * root.root, so if we have switched users, we've got to
502 * update the ownership of the file.
504 if ((debug_flag == 0) &&
505 (mainconfig.radlog_dest == RADLOG_FILES) &&
506 (mainconfig.log_file != NULL)) {
507 chown(mainconfig.log_file, server_uid, server_gid);
514 static const FR_NAME_NUMBER str2dest[] = {
515 { "null", RADLOG_NULL },
516 { "files", RADLOG_FILES },
517 { "syslog", RADLOG_SYSLOG },
518 { "stdout", RADLOG_STDOUT },
519 { "stderr", RADLOG_STDERR },
520 { NULL, RADLOG_NUM_DEST }
527 * This function can ONLY be called from the main server process.
529 int read_mainconfig(int reload)
531 const char *p = NULL;
532 static int old_debug_level = -1;
538 if (stat(radius_dir, &statbuf) < 0) {
539 radlog(L_ERR, "Errors reading %s: %s",
540 radius_dir, strerror(errno));
545 if ((statbuf.st_mode & S_IWOTH) != 0) {
546 radlog(L_ERR, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
553 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
554 radlog(L_ERR, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
561 radlog(L_INFO, "Starting - reading configuration files ...");
563 radlog(L_INFO, "Reloading - reading configuration files...");
566 /* Read the configuration file */
567 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf",
568 radius_dir, mainconfig.name);
569 if ((cs = cf_file_read(buffer)) == NULL) {
570 radlog(L_ERR, "Errors reading %s", buffer);
576 * Add templates to each kind of subsection.
578 templates = cf_section_sub_find(cs, "templates");
580 CONF_SECTION *ts, *mycs;
583 * Loop over the templates, adding them to the
584 * sections in the main configuration file.
586 for (ts = cf_subsection_find_next(templates, NULL, NULL);
588 ts = cf_subsection_find_next(templates, ts, NULL)) {
589 const char *name1 = cf_section_name1(ts);
592 * Loop over sections in the main config
593 * file, adding templats.
595 for (mycs = cf_subsection_find_next(cs, NULL, name1);
597 mycs = cf_subsection_find_next(cs, mycs, name1)) {
600 value = cf_section_value_find(mycs, "template");
604 tts = cf_section_sub_find_name2(templates,
608 radlog(L_ERR, "%s[%d]: Section refers to non-existent template \"%s\"",
609 cf_section_filename(mycs), cf_section_lineno(mycs), value);
612 cf_section_template(mycs, tts);
614 cf_section_template(mycs, ts);
622 * Debug flag 1 MAY go to files.
623 * Debug flag 2 ALWAYS goes to stdout
625 * Parse the log_destination before printing anything else.
626 * All messages before this MUST be errors, which log.c
627 * will print to stderr, since log_file is NULL, too.
629 if (debug_flag < 2) {
631 char *radlog_dest = NULL;
633 rcode = cf_item_parse(cs, "log_destination",
634 PW_TYPE_STRING_PTR, &radlog_dest,
636 if (rcode < 0) return -1;
638 mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest, RADLOG_NUM_DEST);
639 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
640 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
643 cf_section_free(&cs);
647 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
649 * Make sure syslog_facility isn't NULL
652 if (!syslog_facility) {
653 fprintf(stderr, "radiusd: Error: Unknown syslog chosen but no facility spedified\n");
655 cf_section_free(&cs);
658 mainconfig.syslog_facility = fr_str2int(str2fac, syslog_facility, -1);
659 if (mainconfig.syslog_facility < 0) {
660 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
663 free(syslog_facility);
664 cf_section_free(&cs);
671 mainconfig.radlog_dest = RADLOG_STDOUT;
672 mainconfig.radlog_fd = STDOUT_FILENO;
675 /* Initialize the dictionary */
676 cp = cf_pair_find(cs, "dictionary");
677 if (cp) p = cf_pair_value(cp);
678 if (!p) p = radius_dir;
679 DEBUG2("including dictionary file %s/%s", p, RADIUS_DICTIONARY);
680 if (dict_init(p, RADIUS_DICTIONARY) != 0) {
681 radlog(L_ERR, "Errors reading dictionary: %s",
687 * This allows us to figure out where, relative to
688 * radiusd.conf, the other configuration files exist.
690 cf_section_parse(cs, NULL, server_config);
694 * Merge the old with the new.
699 newcs = cf_section_sub_find(cs, "modules");
700 oldcs = cf_section_sub_find(mainconfig.config, "modules");
701 if (newcs && oldcs) {
702 if (!cf_section_migrate(newcs, oldcs)) {
703 radlog(L_ERR, "Fatal error migrating configuration data");
711 * Free the old configuration items, and replace them
714 * Note that where possible, we do atomic switch-overs,
715 * to ensure that the pointers are always valid.
717 cf_section_free(&mainconfig.config);
718 mainconfig.config = cs;
720 clients_parse_section(cs);
722 DEBUG2("%s: #### Loading Realms and Home Servers ####", mainconfig.name);
724 if (!realms_init(cs)) {
729 * Register the %{config:section.subsection} xlat function.
731 xlat_register("config", xlat_config, NULL);
732 xlat_register("client", xlat_client, NULL);
735 * Reload: change debug flag if it's changed in the
736 * configuration file.
739 if (mainconfig.debug_level != old_debug_level) {
740 debug_flag = mainconfig.debug_level;
743 } else if (debug_flag == 0) {
746 * Starting the server, WITHOUT "-x" on the
747 * command-line: use whatever's in the config
750 debug_flag = mainconfig.debug_level;
752 librad_debug = debug_flag;
753 old_debug_level = mainconfig.debug_level;
756 * Go update our behaviour, based on the configuration
761 * The first time around, ensure that we can write to the
764 if (!reload && !debug_flag) {
766 * We need root to do mkdir() and chown(), so we
767 * do this before giving up root.
769 radlogdir_iswritable(uid_name);
773 * We should really switch users earlier in the process.
775 if (!switch_users()) exit(1);
778 * Sanity check the configuration for internal
781 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
782 mainconfig.reject_delay = mainconfig.cleanup_delay;
784 if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
786 /* Reload the modules. */
787 if (setup_modules(reload, mainconfig.config) < 0) {
788 radlog(L_ERR, "Errors initializing modules");
796 * Free the configuration. Called only when the server is exiting.
798 int free_mainconfig(void)
801 * Clean up the configuration data
804 cf_section_free(&mainconfig.config);
806 listen_free(&mainconfig.listen);