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 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>
43 #include <freeradius-devel/request_list.h>
45 #include <sys/resource.h>
51 #ifdef HAVE_SYS_PRTCL_H
52 #include <sys/prctl.h>
59 struct main_config_t mainconfig;
62 * Temporary local variables for parsing the configuration
65 static uid_t server_uid;
66 static gid_t server_gid;
69 * These are not used anywhere else..
71 static const char *localstatedir = NULL;
72 static const char *prefix = NULL;
73 static char *syslog_facility = NULL;
74 static const LRAD_NAME_NUMBER str2fac[] = {
85 { "daemon", LOG_DAEMON },
100 { "cron", LOG_CRON },
103 { "authpriv", LOG_AUTHPRIV },
109 { "local0", LOG_LOCAL0 },
112 { "local1", LOG_LOCAL1 },
115 { "local2", LOG_LOCAL2 },
118 { "local3", LOG_LOCAL3 },
121 { "local4", LOG_LOCAL4 },
124 { "local5", LOG_LOCAL5 },
127 { "local6", LOG_LOCAL6 },
130 { "local7", LOG_LOCAL7 },
136 * Map the proxy server configuration parameters to variables.
138 static const CONF_PARSER proxy_config[] = {
139 { "retry_delay", PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_delay, Stringify(RETRY_DELAY) },
140 { "retry_count", PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_count, Stringify(RETRY_COUNT) },
141 { "default_fallback", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_fallback, "no" },
142 { "dead_time", PW_TYPE_INTEGER, 0, &mainconfig.proxy_dead_time, Stringify(DEAD_TIME) },
143 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN, 0, &mainconfig.wake_all_if_all_dead, "no" },
144 { "proxy_fail_type", PW_TYPE_STRING_PTR, 0, &mainconfig.proxy_fail_type, NULL},
145 { NULL, -1, 0, NULL, NULL }
150 * Security configuration for the server.
152 static const CONF_PARSER security_config[] = {
153 { "max_attributes", PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
154 { "reject_delay", PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
155 { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
156 { NULL, -1, 0, NULL, NULL }
161 * syslog configuration for the server.
163 static const CONF_PARSER log_config[] = {
164 { "syslog_facility", PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
165 { NULL, -1, 0, NULL, NULL }
170 * A mapping of configuration file names to internal variables
172 static const CONF_PARSER server_config[] = {
174 * FIXME: 'prefix' is the ONLY one which should be
175 * configured at compile time. Hard-coding it here is
176 * bad. It will be cleaned up once we clean up the
177 * hard-coded defines for the locations of the various
180 { "prefix", PW_TYPE_STRING_PTR, 0, &prefix, "/usr/local"},
181 { "localstatedir", PW_TYPE_STRING_PTR, 0, &localstatedir, "${prefix}/var"},
182 { "logdir", PW_TYPE_STRING_PTR, 0, &radlog_dir, "${localstatedir}/log"},
183 { "libdir", PW_TYPE_STRING_PTR, 0, &radlib_dir, "${prefix}/lib"},
184 { "radacctdir", PW_TYPE_STRING_PTR, 0, &radacct_dir, "${logdir}/radacct" },
185 { "hostname_lookups", PW_TYPE_BOOLEAN, 0, &librad_dodns, "no" },
187 { "snmp", PW_TYPE_BOOLEAN, 0, &mainconfig.do_snmp, "no" },
189 { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
190 { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
191 { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
192 { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
193 { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &mainconfig.allow_core_dumps, "no" },
194 { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
196 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
197 { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
198 { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
199 { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
200 { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
201 { "user", PW_TYPE_STRING_PTR, 0, &mainconfig.uid_name, NULL},
202 { "group", PW_TYPE_STRING_PTR, 0, &mainconfig.gid_name, NULL},
203 { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
205 { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
207 { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
208 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
209 { "proxy", PW_TYPE_SUBSECTION, 0, NULL, (const void *) proxy_config },
210 { "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
211 { NULL, -1, 0, NULL, NULL }
215 #define MAX_ARGV (256)
217 * Xlat for %{config:section.subsection.attribute}
219 static int xlat_config(void *instance, REQUEST *request,
220 char *fmt, char *out,
222 RADIUS_ESCAPE_STRING func)
227 const char *from, *value;
231 char *argv[MAX_ARGV];
233 request = request; /* -Wunused */
234 instance = instance; /* -Wunused */
240 * Split the string into argv's BEFORE doing radius_xlat...
253 if (argc >= (MAX_ARGV - 1)) break;
256 * Copy the argv over to our buffer.
259 if (to >= myfmt + sizeof(myfmt) - 1) {
260 return 0; /* no error msg */
265 if (from[1] == '{') {
268 length = rad_copy_variable(to, from);
274 } else { /* FIXME: catch %%{ ? */
281 radlog(L_ERR, "config: Unexpected nested '[' in \"%s\"", fmt);
290 radlog(L_ERR, "config: Unbalanced ']' in \"%s\"", fmt);
293 if (from[1] != '.') {
294 radlog(L_ERR, "config: Unexpected text after ']' in \"%s\"", fmt);
303 if (flag == 0) break;
311 if ((*from == '.') && (flag == 0)) {
315 } /* end of string, or found a period */
318 radlog(L_ERR, "config: Unbalanced '[' in \"%s\"", fmt);
322 *(to++) = '\0'; /* terminate the string. */
326 * Expand each string, as appropriate
329 left = sizeof(argv_buf);
330 for (i = 0; i < argc; i++) {
334 * Don't touch argv's which won't be translated.
336 if (strchr(argv[i], '%') == NULL) continue;
338 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
341 * Fail to be backwards compatible.
343 * It's yucky, but it won't break anything,
344 * and it won't cause security problems.
361 cs = cf_section_find(NULL); /* get top-level section */
364 * Root through section & subsection references.
365 * The last entry of argv MUST be the CONF_PAIR.
367 for (i = 0; i < argc - 1; i++) {
372 * FIXME: What about RADIUS attributes containing '['?
374 name2 = strchr(argv[i], '[');
376 char *p = strchr(name2, ']');
377 rad_assert(p != NULL);
378 rad_assert(p[1] =='\0');
385 subcs = cf_section_sub_find_name2(cs, argv[i],
388 radlog(L_ERR, "config: section \"%s %s {}\" not found while dereferencing \"%s\"", argv[i], name2, fmt);
392 subcs = cf_section_sub_find(cs, argv[i]);
394 radlog(L_ERR, "config: section \"%s {}\" not found while dereferencing \"%s\"", argv[i], fmt);
399 } /* until argc - 1 */
402 * This can now have embedded periods in it.
404 cp = cf_pair_find(cs, argv[argc - 1]);
406 radlog(L_ERR, "config: item \"%s\" not found while dereferencing \"%s\"", argv[argc], fmt);
411 * Ensure that we only copy what's necessary.
413 * If 'outlen' is too small, then the output is chopped to fit.
415 value = cf_pair_value(cp);
417 if (outlen > strlen(value)) {
418 outlen = strlen(value) + 1;
422 return func(out, outlen, value);
427 * Recursively make directories.
429 static int r_mkdir(const char *part)
431 char *ptr, parentdir[500];
434 if (stat(part, &st) == 0)
437 ptr = strrchr(part, '/');
442 snprintf(parentdir, (ptr - part)+1, "%s", part);
444 if (r_mkdir(parentdir) != 0)
447 if (mkdir(part, 0770) != 0) {
448 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
456 * Checks if the log directory is writeable by a particular user.
458 static int radlogdir_iswritable(const char *effectiveuser)
460 struct passwd *pwent;
462 if (!radlog_dir || radlog_dir[0] != '/')
465 if (r_mkdir(radlog_dir) != 0)
468 /* FIXME: do we have this function? */
469 if (strstr(radlog_dir, "radius") == NULL)
472 /* we have a logdir that mentions 'radius', so it's probably
473 * safe to chown the immediate directory to be owned by the normal
474 * process owner. we gotta do it before we give up root. -chad
477 if (!effectiveuser) {
481 pwent = getpwnam(effectiveuser);
483 if (pwent == NULL) /* uh oh! */
486 if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
494 * Switch UID and GID to what is specified in the config file
496 static int switch_users(void)
498 struct rlimit core_limits;
501 if (mainconfig.gid_name != NULL) {
504 gr = getgrnam(mainconfig.gid_name);
506 if (errno == ENOMEM) {
507 radlog(L_ERR|L_CONS, "Cannot switch to Group %s: out of memory", mainconfig.gid_name);
509 radlog(L_ERR|L_CONS, "Cannot switch group; %s doesn't exist", mainconfig.gid_name);
513 server_gid = gr->gr_gid;
514 if (setgid(server_gid) < 0) {
515 radlog(L_ERR|L_CONS, "Failed setting Group to %s: %s",
516 mainconfig.gid_name, strerror(errno));
520 server_gid = getgid();
524 if (mainconfig.uid_name != NULL) {
527 pw = getpwnam(mainconfig.uid_name);
529 if (errno == ENOMEM) {
530 radlog(L_ERR|L_CONS, "Cannot switch to User %s: out of memory", mainconfig.uid_name);
532 radlog(L_ERR|L_CONS, "Cannot switch user; %s doesn't exist", mainconfig.uid_name);
536 server_uid = pw->pw_uid;
537 #ifdef HAVE_INITGROUPS
538 if (initgroups(mainconfig.uid_name, server_gid) < 0) {
539 if (errno != EPERM) {
540 radlog(L_ERR|L_CONS, "Failed setting supplementary groups for User %s: %s", mainconfig.uid_name, strerror(errno));
545 if (setuid(server_uid) < 0) {
546 radlog(L_ERR|L_CONS, "Failed setting User to %s: %s", mainconfig.uid_name, strerror(errno));
551 /* Get the current maximum for core files. */
552 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
553 radlog(L_ERR|L_CONS, "Failed to get current core limit: %s", strerror(errno));
557 if (mainconfig.allow_core_dumps) {
558 #ifdef HAVE_SYS_PRTCL_H
559 #ifdef PR_SET_DUMPABLE
560 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
561 radlog(L_ERR|L_CONS,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
567 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
568 radlog(L_ERR|L_CONS, "Cannot update core dump limit: %s",
573 * If we're running as a daemon, and core
574 * dumps are enabled, log that information.
576 } else if ((core_limits.rlim_cur != 0) && !debug_flag)
577 radlog(L_INFO|L_CONS, "Core dumps are enabled.");
579 } else if (!debug_flag) {
581 * Not debugging. Set the core size to zero, to
582 * prevent security breaches. i.e. People
583 * reading passwords from the 'core' file.
585 struct rlimit limits;
588 limits.rlim_max = core_limits.rlim_max;
590 if (setrlimit(RLIMIT_CORE, &limits) < 0) {
591 radlog(L_ERR|L_CONS, "Cannot disable core dumps: %s",
598 * We've probably written to the log file already as
599 * root.root, so if we have switched users, we've got to
600 * update the ownership of the file.
602 if ((debug_flag == 0) &&
603 (mainconfig.radlog_dest == RADLOG_FILES) &&
604 (mainconfig.log_file != NULL)) {
605 chown(mainconfig.log_file, server_uid, server_gid);
613 * Create the linked list of realms from the new configuration type
614 * This way we don't have to change to much in the other source-files
616 static int generate_realms(const char *filename)
619 REALM *my_realms = NULL;
621 char *s, *t, *authhost, *accthost;
625 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
627 cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
628 name2 = cf_section_name2(cs);
630 radlog(L_CONS|L_ERR, "%s[%d]: Missing realm name",
631 filename, cf_section_lineno(cs));
635 * We've found a realm, allocate space for it
637 c = rad_malloc(sizeof(REALM));
638 memset(c, 0, sizeof(REALM));
643 * No authhost means LOCAL.
645 if ((authhost = cf_section_value_find(cs, "authhost")) == NULL) {
646 c->ipaddr.af = AF_INET;
647 c->ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
650 if ((s = strchr(authhost, ':')) != NULL) {
652 c->auth_port = atoi(s);
654 c->auth_port = PW_AUTH_UDP_PORT;
656 if (strcmp(authhost, "LOCAL") == 0) {
658 * Local realms don't have an IP address,
661 c->ipaddr.af = AF_INET;
662 c->ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
665 if (ip_hton(authhost, AF_INET,
667 radlog(L_ERR, "%s[%d]: Host %s not found",
668 filename, cf_section_lineno(cs),
675 * Double check length, just to be sure!
677 if (strlen(authhost) >= sizeof(c->server)) {
678 radlog(L_ERR, "%s[%d]: Server name of length %u is greater than allowed: %u",
679 filename, cf_section_lineno(cs),
681 sizeof(c->server) - 1);
687 * No accthost means LOCAL
689 if ((accthost = cf_section_value_find(cs, "accthost")) == NULL) {
690 c->acct_ipaddr.af = AF_INET;
691 c->acct_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
694 if ((s = strchr(accthost, ':')) != NULL) {
696 c->acct_port = atoi(s);
698 c->acct_port = PW_ACCT_UDP_PORT;
700 if (strcmp(accthost, "LOCAL") == 0) {
702 * Local realms don't have an IP address,
705 c->acct_ipaddr.af = AF_INET;
706 c->acct_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
709 if (ip_hton(accthost, AF_INET,
710 &c->acct_ipaddr) < 0) {
711 radlog(L_ERR, "%s[%d]: Host %s not found",
712 filename, cf_section_lineno(cs),
718 if (strlen(accthost) >= sizeof(c->acct_server)) {
719 radlog(L_ERR, "%s[%d]: Server name of length %u is greater than allowed: %u",
720 filename, cf_section_lineno(cs),
722 sizeof(c->acct_server) - 1);
727 if (strlen(name2) >= sizeof(c->realm)) {
728 radlog(L_ERR, "%s[%d]: Realm name of length %u is greater than allowed %u",
729 filename, cf_section_lineno(cs),
731 sizeof(c->server) - 1);
735 strcpy(c->realm, name2);
736 if (authhost) strcpy(c->server, authhost);
737 if (accthost) strcpy(c->acct_server, accthost);
740 * If one or the other of authentication/accounting
741 * servers is set to LOCALHOST, then don't require
744 rad_assert(c->ipaddr.af == AF_INET);
745 rad_assert(c->acct_ipaddr.af == AF_INET);
746 if ((c->ipaddr.ipaddr.ip4addr.s_addr != htonl(INADDR_NONE)) ||
747 (c->acct_ipaddr.ipaddr.ip4addr.s_addr != htonl(INADDR_NONE))) {
748 if ((s = cf_section_value_find(cs, "secret")) == NULL ) {
749 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
750 filename, cf_section_lineno(cs), name2);
754 if (strlen(s) >= sizeof(c->secret)) {
755 radlog(L_ERR, "%s[%d]: Secret of length %u is greater than the allowed maximum of %u.",
756 filename, cf_section_lineno(cs),
757 strlen(s), sizeof(c->secret) - 1);
760 strlcpy((char *)c->secret, s, sizeof(c->secret));
765 if ((cf_section_value_find(cs, "nostrip")) != NULL)
767 if ((cf_section_value_find(cs, "noacct")) != NULL)
769 if ((cf_section_value_find(cs, "trusted")) != NULL)
771 if ((cf_section_value_find(cs, "notrealm")) != NULL)
773 if ((cf_section_value_find(cs, "notsuffix")) != NULL)
775 if ((t = cf_section_value_find(cs,"ldflag")) != NULL) {
776 static const LRAD_NAME_NUMBER ldflags[] = {
778 { "round_robin", 1 },
782 c->ldflag = lrad_str2int(ldflags, t, -1);
783 if (c->ldflag == -1) {
784 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
785 filename, cf_section_lineno(cs),
791 c->ldflag = 0; /* non, make it fail-over */
794 c->acct_active = TRUE;
802 * And make these realms preferred over the ones
803 * in the 'realms' file.
805 *tail = mainconfig.realms;
806 mainconfig.realms = my_realms;
809 * Ensure that all of the flags agree for the realms.
811 * Yeah, it's O(N^2), but it's only once, and the
812 * maximum number of realms is small.
814 for(c = mainconfig.realms; c != NULL; c = c->next) {
818 * Check that we cannot load balance to LOCAL
819 * realms, as that doesn't make any sense.
821 rad_assert(c->ipaddr.af == AF_INET);
822 rad_assert(c->acct_ipaddr.af == AF_INET);
823 if ((c->ldflag == 1) &&
824 ((c->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_NONE)) ||
825 (c->acct_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_NONE)))) {
826 radlog(L_ERR | L_CONS, "ERROR: Realm %s cannot be load balanced to LOCAL",
832 * Compare this realm to all others, to ensure
833 * that the configuration is consistent.
835 for (this = c->next; this != NULL; this = this->next) {
836 if (strcasecmp(c->realm, this->realm) != 0) {
841 * Same realm: Different load balancing
844 if (c->ldflag != this->ldflag) {
845 radlog(L_ERR | L_CONS, "ERROR: Inconsistent value in realm %s for load balancing 'ldflag' attribute",
856 static const LRAD_NAME_NUMBER str2dest[] = {
857 { "null", RADLOG_NULL },
858 { "files", RADLOG_FILES },
859 { "syslog", RADLOG_SYSLOG },
860 { "stdout", RADLOG_STDOUT },
861 { "stderr", RADLOG_STDERR },
862 { NULL, RADLOG_NUM_DEST }
869 * This function can ONLY be called from the main server process.
871 int read_mainconfig(int reload)
873 static int old_debug_level = -1;
875 CONF_SECTION *cs, *oldcs;
876 rad_listen_t *listener;
879 if (stat(radius_dir, &statbuf) < 0) {
880 radlog(L_ERR|L_CONS, "Errors reading %s: %s",
881 radius_dir, strerror(errno));
885 if ((statbuf.st_mode & S_IWOTH) != 0) {
886 radlog(L_ERR|L_CONS, "Configuration directory %s is globally writable. Refusing to start due to insecure configuration.",
892 if (0 && (statbuf.st_mode & S_IROTH) != 0) {
893 radlog(L_ERR|L_CONS, "Configuration directory %s is globally readable. Refusing to start due to insecure configuration.",
898 /* Read the configuration file */
899 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
900 radius_dir, mainconfig.radiusd_conf);
901 if ((cs = cf_file_read(buffer)) == NULL) {
902 radlog(L_ERR|L_CONS, "Errors reading %s", buffer);
907 * Debug flag 1 MAY go to files.
908 * Debug flag 2 ALWAYS goes to stdout
910 * Parse the log_destination before printing anything else.
911 * All messages before this MUST be errors, which log.c
912 * will print to stderr, since log_file is NULL, too.
914 if (debug_flag < 2) {
916 char *radlog_dest = NULL;
918 rcode = cf_item_parse(cs, "log_destination",
919 PW_TYPE_STRING_PTR, &radlog_dest,
921 if (rcode < 0) return -1;
923 mainconfig.radlog_dest = lrad_str2int(str2dest, radlog_dest, RADLOG_NUM_DEST);
924 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
925 fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
928 cf_section_free(&cs);
932 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
933 static const CONF_PARSER syslog_config[] = {
934 { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config},
935 { NULL, -1, 0, NULL, NULL }
937 cf_section_parse(cs, NULL, syslog_config);
940 * Make sure syslog_facility isn't NULL before using it
942 if (!syslog_facility) {
943 fprintf(stderr, "radiusd: Error: Unknown syslog chosen but no facility spedified\n");
945 cf_section_free(&cs);
948 mainconfig.syslog_facility = lrad_str2int(str2fac, syslog_facility, -1);
949 if (mainconfig.syslog_facility < 0) {
950 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
953 free(syslog_facility);
954 cf_section_free(&cs);
959 if (mainconfig.radlog_dest == RADLOG_FILES) {
960 static const CONF_PARSER file_config[] = {
961 { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
962 { NULL, -1, 0, NULL, NULL }
965 cf_section_parse(cs, NULL, file_config);
970 mainconfig.radlog_dest = RADLOG_STDOUT;
971 mainconfig.radlog_fd = STDOUT_FILENO;
974 radlog(L_INFO, "%s", radiusd_version);
976 radlog(L_INFO, "Starting - reading configuration files ...");
978 radlog(L_INFO, "Reloading - reading configuration files...");
981 /* Initialize the dictionary */
982 DEBUG2("read_config_files: reading dictionary");
983 if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
984 radlog(L_ERR|L_CONS, "Errors reading dictionary: %s",
990 * This allows us to figure out where, relative to
991 * radiusd.conf, the other configuration files exist.
993 cf_section_parse(cs, NULL, server_config);
997 * Merge the old with the new.
1000 CONF_SECTION *newcs;
1002 newcs = cf_section_sub_find(cs, "modules");
1003 oldcs = cf_section_sub_find(mainconfig.config, "modules");
1004 if (newcs && oldcs) {
1005 if (!cf_section_migrate(newcs, oldcs)) {
1006 radlog(L_ERR|L_CONS, "Fatal error migrating configuration data");
1014 * Free the old configuration items, and replace them
1015 * with the new ones.
1017 * Note that where possible, we do atomic switch-overs,
1018 * to ensure that the pointers are always valid.
1020 oldcs = mainconfig.config;
1021 mainconfig.config = cs;
1022 cf_section_free(&oldcs);
1025 * Old-style realms file.
1027 snprintf(buffer, sizeof(buffer), "%.200s/%.50s", radius_dir, RADIUS_REALMS);
1028 DEBUG2("read_config_files: reading realms");
1029 if (read_realms_file(buffer) < 0) {
1030 radlog(L_ERR|L_CONS, "Errors reading realms");
1035 * If there isn't any realms it isn't fatal..
1037 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
1038 radius_dir, mainconfig.radiusd_conf);
1039 if (generate_realms(buffer) < 0) {
1044 * Register the %{config:section.subsection} xlat function.
1046 xlat_register("config", xlat_config, NULL);
1049 * Set the libraries debugging flag to whatever the main
1050 * flag is. Note that on a SIGHUP, to turn the debugging
1051 * off, we do other magic.
1053 * Increase the debug level, if the configuration file
1054 * says to, OR, if we're decreasing the debug from what it
1055 * was before, allow that, too.
1057 if ((mainconfig.debug_level > debug_flag) ||
1058 (mainconfig.debug_level <= old_debug_level)) {
1059 debug_flag = mainconfig.debug_level;
1061 librad_debug = debug_flag;
1062 old_debug_level = mainconfig.debug_level;
1065 * Go update our behaviour, based on the configuration
1070 * The first time around, ensure that we can write to the
1075 * We need root to do mkdir() and chown(), so we
1076 * do this before giving up root.
1078 radlogdir_iswritable(mainconfig.uid_name);
1082 * We should really switch users earlier in the process.
1087 * Sanity check the configuration for internal
1090 if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
1091 mainconfig.reject_delay = mainconfig.cleanup_delay;
1095 * Initialize the old "bind_address" and "port", first.
1100 * Read the list of listeners.
1102 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
1103 radius_dir, mainconfig.radiusd_conf);
1104 if (listen_init(buffer, &listener) < 0) {
1109 radlog(L_ERR|L_CONS, "Server is not configured to listen on any ports. Exiting.");
1113 listen_free(&mainconfig.listen);
1114 mainconfig.listen = listener;
1117 * Walk through the listeners. If we're listening on acct
1118 * or auth, read in the clients files, else ignore them.
1120 for (listener = mainconfig.listen;
1122 listener = listener->next) {
1123 if ((listener->type == RAD_LISTEN_AUTH) ||
1124 (listener->type == RAD_LISTEN_ACCT)) {
1129 if (listener != NULL) {
1130 RADCLIENT_LIST *clients, *old_clients;
1133 * Create the new clients first, and add them
1134 * to the CONF_SECTION, where they're automagically
1135 * freed if anything goes wrong.
1137 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
1138 radius_dir, mainconfig.radiusd_conf);
1139 clients = clients_parse_section(buffer, mainconfig.config);
1145 * Free the old trees AFTER replacing them with
1148 old_clients = mainconfig.clients;
1149 mainconfig.clients = clients;
1150 clients_free(old_clients);
1155 /* Reload the modules. */
1156 DEBUG2("radiusd: entering modules setup");
1157 if (setup_modules(reload) < 0) {
1158 radlog(L_ERR|L_CONS, "Errors setting up modules");
1165 * Free the configuration. Called only when the server is exiting.
1167 int free_mainconfig(void)
1170 * Clean up the configuration data
1173 cf_section_free(&mainconfig.config);
1174 free(mainconfig.radiusd_conf);
1175 realm_free(mainconfig.realms);
1176 listen_free(&mainconfig.listen);