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_condition = NULL;
65 typedef struct cached_config_t {
66 struct cached_config_t *next;
71 static cached_config_t *cs_cache = NULL;
74 * Temporary local variables for parsing the configuration
79 * Systems that have set/getresuid also have setuid.
82 static gid_t server_gid;
83 static const char *uid_name = NULL;
84 static const char *gid_name = NULL;
86 static const char *chroot_dir = NULL;
87 static int allow_core_dumps = 0;
88 static const char *radlog_dest = NULL;
91 * These are not used anywhere else..
93 static const char *localstatedir = NULL;
94 static const char *prefix = NULL;
95 static char *syslog_facility = NULL;
96 static const FR_NAME_NUMBER str2fac[] = {
101 { "user", LOG_USER },
104 { "mail", LOG_MAIL },
107 { "daemon", LOG_DAEMON },
110 { "auth", LOG_AUTH },
116 { "news", LOG_NEWS },
119 { "uucp", LOG_UUCP },
122 { "cron", LOG_CRON },
125 { "authpriv", LOG_AUTHPRIV },
131 { "local0", LOG_LOCAL0 },
134 { "local1", LOG_LOCAL1 },
137 { "local2", LOG_LOCAL2 },
140 { "local3", LOG_LOCAL3 },
143 { "local4", LOG_LOCAL4 },
146 { "local5", LOG_LOCAL5 },
149 { "local6", LOG_LOCAL6 },
152 { "local7", LOG_LOCAL7 },
158 * Security configuration for the server.
160 static const CONF_PARSER security_config[] = {
161 { "max_attributes", PW_TYPE_INTEGER, 0, &fr_max_attributes, Stringify(0) },
162 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
163 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
164 { NULL, -1, 0, NULL, NULL }
169 * Logging configuration for the server.
171 static const CONF_PARSER logdest_config[] = {
172 { "destination", PW_TYPE_STRING_PTR, 0, &radlog_dest, "files" },
173 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
175 { "file", PW_TYPE_STRING_PTR, 0, &mainconfig.log_file, "${logdir}/radius.log" },
176 { "requests", PW_TYPE_STRING_PTR, 0, &request_log_file, NULL },
177 { NULL, -1, 0, NULL, NULL }
181 static const CONF_PARSER serverdest_config[] = {
182 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) logdest_config },
183 { "log_file", PW_TYPE_STRING_PTR, 0, &mainconfig.log_file, NULL },
184 { "log_destination", PW_TYPE_STRING_PTR, 0, &radlog_dest, NULL },
185 { NULL, -1, 0, NULL, NULL }
189 static const CONF_PARSER log_config_nodest[] = {
190 { "stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
192 { "auth", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth, "no" },
193 { "auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
194 { "auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
195 { "msg_badpass", PW_TYPE_STRING_PTR, 0, &mainconfig.auth_badpass_msg, NULL},
196 { "msg_goodpass", PW_TYPE_STRING_PTR, 0, &mainconfig.auth_goodpass_msg, NULL},
198 { NULL, -1, 0, NULL, NULL }
203 * A mapping of configuration file names to internal variables
205 static const CONF_PARSER server_config[] = {
207 * FIXME: 'prefix' is the ONLY one which should be
208 * configured at compile time. Hard-coding it here is
209 * bad. It will be cleaned up once we clean up the
210 * hard-coded defines for the locations of the various
213 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
214 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
215 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
216 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
217 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
218 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &fr_dns_lookups, "no" },
219 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
220 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
221 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
222 #ifdef DELETE_BLOCKED_REQUESTS
223 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
225 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &allow_core_dumps, "no" },
227 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
228 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
230 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
233 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
235 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config_nodest },
238 * People with old configs will have these. They are listed
239 * AFTER the "log" section, so if they exist in radiusd.conf,
240 * it will prefer "log_foo = bar" to "log { foo = bar }".
241 * They're listed with default values of NULL, so that if they
242 * DON'T exist in radiusd.conf, then the previously parsed
243 * values for "log { foo = bar}" will be used.
245 { "log_auth", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth, NULL },
246 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, NULL },
247 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, NULL },
248 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names, NULL },
250 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
252 { NULL, -1, 0, NULL, NULL }
255 #define MAX_ARGV (256)
258 static size_t config_escape_func(char *out, size_t outlen, const char *in)
261 static const char *disallowed = "%{}\\'\"`";
265 * Non-printable characters get replaced with their
266 * mime-encoded equivalents.
269 if (outlen <= 3) break;
271 snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
278 } else if (strchr(disallowed, *in) != NULL) {
279 if (outlen <= 2) break;
291 * Only one byte left.
311 * Xlat for %{config:section.subsection.attribute}
313 static size_t xlat_config(void *instance, REQUEST *request,
314 char *fmt, char *out,
316 RADIUS_ESCAPE_STRING func)
323 request = request; /* -Wunused */
324 instance = instance; /* -Wunused */
329 if (!radius_xlat(buffer, sizeof(buffer), fmt, request, config_escape_func)) {
333 ci = cf_reference_item(request->root->config,
334 request->root->config, buffer);
335 if (!ci || !cf_item_is_pair(ci)) {
340 cp = cf_itemtopair(ci);
343 * Ensure that we only copy what's necessary.
345 * If 'outlen' is too small, then the output is chopped to fit.
347 value = cf_pair_value(cp);
349 if (outlen > strlen(value)) {
350 outlen = strlen(value) + 1;
354 return func(out, outlen, value);
359 * Xlat for %{client:foo}
361 static size_t xlat_client(UNUSED void *instance, REQUEST *request,
362 char *fmt, char *out,
364 UNUSED RADIUS_ESCAPE_STRING func)
366 const char *value = NULL;
369 if (!fmt || !out || (outlen < 1)) return 0;
371 if (!request || !request->client) {
376 cp = cf_pair_find(request->client->cs, fmt);
377 if (!cp || !(value = cf_pair_value(cp))) {
382 strlcpy(out, value, outlen);
388 * Recursively make directories.
390 static int r_mkdir(const char *part)
392 char *ptr, parentdir[500];
395 if (stat(part, &st) == 0)
398 ptr = strrchr(part, FR_DIR_SEP);
403 snprintf(parentdir, (ptr - part)+1, "%s", part);
405 if (r_mkdir(parentdir) != 0)
408 if (mkdir(part, 0770) != 0) {
409 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
418 int did_setuid = FALSE;
420 #if defined(HAVE_SETRESUID) && defined (HAVE_GETRESUID)
421 void fr_suid_up(void)
423 uid_t ruid, euid, suid;
425 if (getresuid(&ruid, &euid, &suid) < 0) {
426 radlog(L_ERR, "Failed getting saved UID's");
430 if (setresuid(-1, suid, -1) < 0) {
431 radlog(L_ERR, "Failed switching to privileged user");
435 if (geteuid() != suid) {
436 radlog(L_ERR, "Switched to unknown UID");
441 void fr_suid_down(void)
443 if (!did_setuid) return;
445 if (setresuid(-1, server_uid, geteuid()) < 0) {
446 fprintf(stderr, "%s: Failed switching to uid %s: %s\n",
447 progname, uid_name, strerror(errno));
451 if (geteuid() != server_uid) {
452 fprintf(stderr, "%s: Failed switching uid: UID is incorrect\n",
458 void fr_suid_down_permanent(void)
460 uid_t ruid, euid, suid;
462 if (!did_setuid) return;
464 if (getresuid(&ruid, &euid, &suid) < 0) {
465 radlog(L_ERR, "Failed getting saved uid's");
469 if (setresuid(server_uid, server_uid, server_uid) < 0) {
470 radlog(L_ERR, "Failed in permanent switch to uid %s: %s",
471 uid_name, strerror(errno));
475 if (geteuid() != server_uid) {
476 radlog(L_ERR, "Switched to unknown uid");
481 if (getresuid(&ruid, &euid, &suid) < 0) {
482 radlog(L_ERR, "Failed getting saved uid's: %s",
489 * Much less secure...
491 void fr_suid_up(void)
494 void fr_suid_down(void)
496 if (!uid_name) return;
498 if (setuid(server_uid) < 0) {
499 fprintf(stderr, "%s: Failed switching to uid %s: %s\n",
500 progname, uid_name, strerror(errno));
504 void fr_suid_down_permanent(void)
510 * Do chroot, if requested.
512 * Switch UID and GID to what is specified in the config file
514 static int switch_users(CONF_SECTION *cs)
518 #ifdef HAVE_SYS_RESOURCE_H
519 struct rlimit core_limits;
523 * Don't do chroot/setuid/setgid if we're in debugging
526 if (debug_flag && (getuid() != 0)) return 1;
530 cp = cf_pair_find(cs, "group");
531 if (cp) gid_name = cf_pair_value(cp);
535 DEBUG2("group = %s", gid_name);
536 gr = getgrnam(gid_name);
538 fprintf(stderr, "%s: Cannot get ID for group %s: %s\n",
539 progname, gid_name, strerror(errno));
542 server_gid = gr->gr_gid;
544 server_gid = getgid();
550 cp = cf_pair_find(cs, "user");
551 if (cp) uid_name = cf_pair_value(cp);
555 DEBUG2("user = %s", uid_name);
556 pw = getpwnam(uid_name);
558 fprintf(stderr, "%s: Cannot get passwd entry for user %s: %s\n",
559 progname, uid_name, strerror(errno));
562 server_uid = pw->pw_uid;
563 #ifdef HAVE_INITGROUPS
564 if (initgroups(uid_name, server_gid) < 0) {
565 fprintf(stderr, "%s: Cannot initialize supplementary group list for user %s: %s\n",
566 progname, uid_name, strerror(errno));
571 server_uid = getuid();
575 cp = cf_pair_find(cs, "chroot");
576 if (cp) chroot_dir = cf_pair_value(cp);
578 DEBUG2("chroot = %s", chroot_dir);
579 if (chroot(chroot_dir) < 0) {
580 fprintf(stderr, "%s: Failed to perform chroot %s: %s",
581 progname, chroot_dir, strerror(errno));
586 * Note that we leave chdir alone. It may be
587 * OUTSIDE of the root. This allows us to read
588 * the configuration from "-d ./etc/raddb", with
589 * the chroot as "./chroot/" for example. After
590 * the server has been loaded, it does a "cd
591 * ${logdir}" below, so that core files (if any)
592 * go to a logging directory.
594 * This also allows the configuration of the
595 * server to be outside of the chroot. If the
596 * server is statically linked, then the only
597 * things needed inside of the chroot are the
598 * logging directories.
600 radlog(L_INFO, "performing chroot to %s\n", chroot_dir);
605 if (gid_name && (setgid(server_gid) < 0)) {
606 fprintf(stderr, "%s: Failed setting group to %s: %s",
607 progname, gid_name, strerror(errno));
617 * Now core dumps are disabled on most secure systems.
625 * Double check that we can write to the log directory.
627 * If we can't, don't start, as we can't log any errors!
629 if ((mainconfig.radlog_dest == RADLOG_FILES) &&
630 (mainconfig.log_file != NULL)) {
631 int fd = open(mainconfig.log_file,
632 O_WRONLY | O_APPEND | O_CREAT, 0640);
634 fprintf(stderr, "%s: Cannot write to log file %s: %s\n",
635 progname, mainconfig.log_file, strerror(errno));
641 * After this it's safe to call radlog(), as it's going
642 * to the right place.
646 #ifdef HAVE_SYS_RESOURCE_H
647 /* Get the current maximum for core files. */
648 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
649 radlog(L_ERR, "Failed to get current core limit: %s", strerror(errno));
655 * Core dumps are allowed if we're in debug mode, OR
656 * we've allowed them, OR we did a setuid (which turns
659 * Otherwise, disable core dumps for security.
662 if (!(debug_flag || allow_core_dumps || did_setuid)) {
663 #ifdef HAVE_SYS_RESOURCE_H
664 struct rlimit no_core;
666 no_core.rlim_cur = 0;
667 no_core.rlim_max = 0;
669 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
670 radlog(L_ERR, "Failed disabling core dumps: %s",
677 * Otherwise, re-enable core dumps if we're
678 * running as a daemon, AND core dumps are
679 * allowed, AND we changed UID's.
681 } else if ((debug_flag == 0) && allow_core_dumps && did_setuid) {
683 * Set the dumpable flag.
685 #ifdef HAVE_SYS_PRCTL_H
686 #ifdef PR_SET_DUMPABLE
687 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
688 radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
695 * Reset the core dump limits again, just to
696 * double check that they haven't changed.
698 #ifdef HAVE_SYS_RESOURCE_H
699 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
700 radlog(L_ERR, "Cannot update core dump limit: %s",
706 radlog(L_INFO, "Core dumps are enabled.");
709 * Else we're debugging (so core dumps are enabled)
710 * OR we're not debugging, AND "allow_core_dumps == FALSE",
711 * OR we're not debugging, AND core dumps are allowed,
712 * BUT we didn't call setuid, so we haven't changed the
713 * core dump capabilities inherited from the parent shell.
721 static const FR_NAME_NUMBER str2dest[] = {
722 { "null", RADLOG_NULL },
723 { "files", RADLOG_FILES },
724 { "syslog", RADLOG_SYSLOG },
725 { "stdout", RADLOG_STDOUT },
726 { "stderr", RADLOG_STDERR },
727 { NULL, RADLOG_NUM_DEST }
734 * This function can ONLY be called from the main server process.
736 int read_mainconfig(int reload)
738 const char *p = NULL;
746 radlog(L_ERR, "Reload is not implemented");
750 if (stat(radius_dir, &statbuf) < 0) {
751 radlog(L_ERR, "Errors reading %s: %s",
752 radius_dir, strerror(errno));
757 if ((statbuf.st_mode & S_IWOTH) != 0) {
758 radlog(L_ERR, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
765 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
766 radlog(L_ERR, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
772 radlog(L_INFO, "Starting - reading configuration files ...");
774 /* Read the configuration file */
775 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf",
776 radius_dir, mainconfig.name);
777 if ((cs = cf_file_read(buffer)) == NULL) {
778 radlog(L_ERR, "Errors reading %s", buffer);
783 * If there was no log destination set on the command line,
786 if (mainconfig.radlog_dest == RADLOG_NULL) {
787 if (cf_section_parse(cs, NULL, serverdest_config) < 0) {
788 fprintf(stderr, "radiusd: Error: Failed to parse log{} section.\n");
789 cf_section_free(&cs);
794 fprintf(stderr, "radiusd: Error: No log destination specified.\n");
795 cf_section_free(&cs);
799 mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest,
801 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
802 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
804 cf_section_free(&cs);
808 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
810 * Make sure syslog_facility isn't NULL
813 if (!syslog_facility) {
814 fprintf(stderr, "radiusd: Error: Syslog chosen but no facility was specified\n");
815 cf_section_free(&cs);
818 mainconfig.syslog_facility = fr_str2int(str2fac, syslog_facility, -1);
819 if (mainconfig.syslog_facility < 0) {
820 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
822 cf_section_free(&cs);
830 * Switch users as early as possible.
832 if (!switch_users(cs)) exit(1);
835 /* Initialize the dictionary */
836 cp = cf_pair_find(cs, "dictionary");
837 if (cp) p = cf_pair_value(cp);
838 if (!p) p = radius_dir;
839 DEBUG2("including dictionary file %s/%s", p, RADIUS_DICTIONARY);
840 if (dict_init(p, RADIUS_DICTIONARY) != 0) {
841 radlog(L_ERR, "Errors reading dictionary: %s",
847 * This allows us to figure out where, relative to
848 * radiusd.conf, the other configuration files exist.
850 cf_section_parse(cs, NULL, server_config);
853 * Free the old configuration items, and replace them
856 * Note that where possible, we do atomic switch-overs,
857 * to ensure that the pointers are always valid.
859 cf_section_free(&mainconfig.config);
860 mainconfig.config = cs;
862 DEBUG2("%s: #### Loading Realms and Home Servers ####", mainconfig.name);
863 if (!realms_init(cs)) {
867 DEBUG2("%s: #### Loading Clients ####", mainconfig.name);
868 if (!clients_parse_section(cs)) {
873 * Register the %{config:section.subsection} xlat function.
875 xlat_register("config", xlat_config, NULL);
876 xlat_register("client", xlat_client, NULL);
879 * Starting the server, WITHOUT "-x" on the
880 * command-line: use whatever is in the config
883 if (debug_flag == 0) {
884 debug_flag = mainconfig.debug_level;
886 fr_debug_flag = debug_flag;
889 * Go update our behaviour, based on the configuration
894 * Sanity check the configuration for internal
897 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
898 mainconfig.reject_delay = mainconfig.cleanup_delay;
900 if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
902 /* Reload the modules. */
903 if (setup_modules(reload, mainconfig.config) < 0) {
908 if (chdir(radlog_dir) < 0) {
909 radlog(L_ERR, "Failed to 'chdir %s' after chroot: %s",
910 radlog_dir, strerror(errno));
915 cc = rad_malloc(sizeof(*cc));
916 memset(cc, 0, sizeof(*cc));
919 rad_assert(cs_cache == NULL);
926 * Free the configuration. Called only when the server is exiting.
928 int free_mainconfig(void)
930 cached_config_t *cc, *next;
932 virtual_servers_free(0);
935 * Free all of the cached configurations.
937 for (cc = cs_cache; cc != NULL; cc = next) {
939 cf_section_free(&cc->cs);
944 * Clean up the configuration data
948 listen_free(&mainconfig.listen);
954 void hup_mainconfig(void)
960 /* Read the configuration file */
961 snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf",
962 radius_dir, mainconfig.name);
963 if ((cs = cf_file_read(buffer)) == NULL) {
964 radlog(L_ERR, "Failed to re-read %s", buffer);
968 cc = rad_malloc(sizeof(*cc));
969 memset(cc, 0, sizeof(*cc));
972 * Save the current configuration. Note that we do NOT
973 * free older ones. We should probably do so at some
974 * point. Doing so will require us to mark which modules
975 * are still in use, and which aren't. Modules that
976 * can't be HUPed always use the original configuration.
977 * Modules that can be HUPed use one of the newer
980 cc->created = time(NULL);
986 * Prefer the new module configuration.
988 module_hup(cf_section_sub_find(cs, "modules"));
991 * Load new servers BEFORE freeing old ones.
993 virtual_servers_load(cs);
995 virtual_servers_free(cc->created - mainconfig.max_request_time * 4);