Allow controllable log file output, based on xlat.
[freeradius.git] / src / main / mainconfig.c
1 /*
2  * mainconf.c   Handle the server's configuration.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2002,2006-2007  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/rad_assert.h>
30
31 #include <sys/stat.h>
32
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
35 #endif
36
37 #ifdef HAVE_PWD_H
38 #include <pwd.h>
39 #endif
40
41 #ifdef HAVE_GRP_H
42 #include <grp.h>
43 #endif
44
45 #ifdef HAVE_SYS_PRCTL_H
46 #include <sys/prctl.h>
47 #endif
48
49 #ifdef HAVE_SYSLOG_H
50 #       include <syslog.h>
51 #endif
52
53 #ifdef HAVE_SYS_STAT_H
54 #include <sys/stat.h>
55 #endif
56
57 #ifdef HAVE_FCNTL_H
58 #include <fcntl.h>
59 #endif
60
61 struct main_config_t mainconfig;
62 char *request_log_file = NULL;
63
64 /*
65  *      Temporary local variables for parsing the configuration
66  *      file.
67  */
68 static uid_t server_uid;
69 static gid_t server_gid;
70 static const char *uid_name = NULL;
71 static const char *gid_name = NULL;
72 static const char *chroot_dir = NULL;
73 static int allow_core_dumps = 0;
74 static const char *radlog_dest = NULL;
75
76 /*
77  *      These are not used anywhere else..
78  */
79 static const char *localstatedir = NULL;
80 static const char *prefix = NULL;
81 static char *syslog_facility = NULL;
82 static const FR_NAME_NUMBER str2fac[] = {
83 #ifdef LOG_KERN
84         { "kern", LOG_KERN },
85 #endif
86 #ifdef LOG_USER
87         { "user", LOG_USER },
88 #endif
89 #ifdef LOG_MAIL
90         { "mail", LOG_MAIL },
91 #endif
92 #ifdef LOG_DAEMON
93         { "daemon", LOG_DAEMON },
94 #endif
95 #ifdef LOG_AUTH
96         { "auth", LOG_AUTH },
97 #endif
98 #ifdef LOG_LPR
99         { "lpr", LOG_LPR },
100 #endif
101 #ifdef LOG_NEWS
102         { "news", LOG_NEWS },
103 #endif
104 #ifdef LOG_UUCP
105         { "uucp", LOG_UUCP },
106 #endif
107 #ifdef LOG_CRON
108         { "cron", LOG_CRON },
109 #endif
110 #ifdef LOG_AUTHPRIV
111         { "authpriv", LOG_AUTHPRIV },
112 #endif
113 #ifdef LOG_FTP
114         { "ftp", LOG_FTP },
115 #endif
116 #ifdef LOG_LOCAL0
117         { "local0", LOG_LOCAL0 },
118 #endif
119 #ifdef LOG_LOCAL1
120         { "local1", LOG_LOCAL1 },
121 #endif
122 #ifdef LOG_LOCAL2
123         { "local2", LOG_LOCAL2 },
124 #endif
125 #ifdef LOG_LOCAL3
126         { "local3", LOG_LOCAL3 },
127 #endif
128 #ifdef LOG_LOCAL4
129         { "local4", LOG_LOCAL4 },
130 #endif
131 #ifdef LOG_LOCAL5
132         { "local5", LOG_LOCAL5 },
133 #endif
134 #ifdef LOG_LOCAL6
135         { "local6", LOG_LOCAL6 },
136 #endif
137 #ifdef LOG_LOCAL7
138         { "local7", LOG_LOCAL7 },
139 #endif
140         { NULL, -1 }
141 };
142
143 /*
144  *  Security configuration for the server.
145  */
146 static const CONF_PARSER security_config[] = {
147         { "max_attributes",  PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
148         { "reject_delay",  PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
149         { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
150         { NULL, -1, 0, NULL, NULL }
151 };
152
153
154 /*
155  *      Logging configuration for the server.
156  */
157 static const CONF_PARSER logdest_config[] = {
158         { "destination",  PW_TYPE_STRING_PTR, 0, &radlog_dest, "files" },
159         { "syslog_facility",  PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
160
161         { "file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
162         { "requests", PW_TYPE_STRING_PTR, -1, &request_log_file, NULL },
163         { NULL, -1, 0, NULL, NULL }
164 };
165
166
167 static const CONF_PARSER serverdest_config[] = {
168         { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) logdest_config },
169         { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, NULL },
170         { "log_destination", PW_TYPE_STRING_PTR, -1, &radlog_dest, NULL },
171         { NULL, -1, 0, NULL, NULL }
172 };
173
174
175 static const CONF_PARSER log_config_nodest[] = {
176         { "stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
177
178         { "auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
179         { "auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
180         { "auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
181
182         { NULL, -1, 0, NULL, NULL }
183 };
184
185
186 /*
187  *  A mapping of configuration file names to internal variables
188  */
189 static const CONF_PARSER server_config[] = {
190         /*
191          *      FIXME: 'prefix' is the ONLY one which should be
192          *      configured at compile time.  Hard-coding it here is
193          *      bad.  It will be cleaned up once we clean up the
194          *      hard-coded defines for the locations of the various
195          *      files.
196          */
197         { "prefix",             PW_TYPE_STRING_PTR, 0, &prefix,            "/usr/local"},
198         { "localstatedir",      PW_TYPE_STRING_PTR, 0, &localstatedir,     "${prefix}/var"},
199         { "logdir",             PW_TYPE_STRING_PTR, 0, &radlog_dir,        "${localstatedir}/log"},
200         { "libdir",             PW_TYPE_STRING_PTR, 0, &radlib_dir,        "${prefix}/lib"},
201         { "radacctdir",         PW_TYPE_STRING_PTR, 0, &radacct_dir,       "${logdir}/radacct" },
202         { "hostname_lookups",   PW_TYPE_BOOLEAN,    0, &librad_dodns,      "no" },
203         { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
204         { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
205         { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
206 #ifdef DELETE_BLOCKED_REQUESTS
207         { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
208 #endif
209         { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &allow_core_dumps, "no" },
210
211         { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
212         { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
213
214         { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
215
216 #ifdef WITH_PROXY
217         { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
218 #endif
219         { "log", PW_TYPE_SUBSECTION, 0, NULL, (const void *) log_config_nodest },
220
221         /*
222          *      People with old configs will have these.  They are listed
223          *      AFTER the "log" section, so if they exist in radiusd.conf,
224          *      it will prefer "log_foo = bar" to "log { foo = bar }".
225          *      They're listed with default values of NULL, so that if they
226          *      DON'T exist in radiusd.conf, then the previously parsed
227          *      values for "log { foo = bar}" will be used.
228          */
229         { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, NULL },
230         { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, NULL },
231         { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, NULL },
232         { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names, NULL },
233
234         {  "security", PW_TYPE_SUBSECTION, 0, NULL, (const void *) security_config },
235
236         { NULL, -1, 0, NULL, NULL }
237 };
238
239 #define MAX_ARGV (256)
240
241
242 static size_t config_escape_func(char *out, size_t outlen, const char *in)
243 {
244         size_t len = 0;
245         static const char *disallowed = "%{}\\'\"`";
246
247         while (in[0]) {
248                 /*
249                  *      Non-printable characters get replaced with their
250                  *      mime-encoded equivalents.
251                  */
252                 if ((in[0] < 32)) {
253                         if (outlen <= 3) break;
254
255                         snprintf(out, outlen, "=%02X", (unsigned char) in[0]);
256                         in++;
257                         out += 3;
258                         outlen -= 3;
259                         len += 3;
260                         continue;
261
262                 } else if (strchr(disallowed, *in) != NULL) {
263                         if (outlen <= 2) break;
264
265                         out[0] = '\\';
266                         out[1] = *in;
267                         in++;
268                         out += 2;
269                         outlen -= 2;
270                         len += 2;
271                         continue;
272                 }
273
274                 /*
275                  *      Only one byte left.
276                  */
277                 if (outlen <= 1) {
278                         break;
279                 }
280
281                 /*
282                  *      Allowed character.
283                  */
284                 *out = *in;
285                 out++;
286                 in++;
287                 outlen--;
288                 len++;
289         }
290         *out = '\0';
291         return len;
292 }
293
294 /*
295  *      Xlat for %{config:section.subsection.attribute}
296  */
297 static size_t xlat_config(void *instance, REQUEST *request,
298                           char *fmt, char *out,
299                           size_t outlen,
300                           RADIUS_ESCAPE_STRING func)
301 {
302         const char *value;
303         CONF_PAIR *cp;
304         CONF_ITEM *ci;
305         char buffer[1024];
306
307         request = request;      /* -Wunused */
308         instance = instance;    /* -Wunused */
309
310         /*
311          *      Expand it safely.
312          */
313         if (!radius_xlat(buffer, sizeof(buffer), fmt, request, config_escape_func)) {
314                 return 0;
315         }
316
317         ci = cf_reference_item(request->root->config,
318                                request->root->config, buffer);
319         if (!ci || !cf_item_is_pair(ci)) {
320                 *out = '\0';
321                 return 0;
322         }
323
324         cp = cf_itemtopair(ci);
325
326         /*
327          *  Ensure that we only copy what's necessary.
328          *
329          *  If 'outlen' is too small, then the output is chopped to fit.
330          */
331         value = cf_pair_value(cp);
332         if (value) {
333                 if (outlen > strlen(value)) {
334                         outlen = strlen(value) + 1;
335                 }
336         }
337
338         return func(out, outlen, value);
339 }
340
341
342 /*
343  *      Xlat for %{client:foo}
344  */
345 static size_t xlat_client(UNUSED void *instance, REQUEST *request,
346                        char *fmt, char *out,
347                        size_t outlen,
348                        UNUSED RADIUS_ESCAPE_STRING func)
349 {
350         const char *value = NULL;
351         CONF_PAIR *cp;
352
353         if (!fmt || !out || (outlen < 1)) return 0;
354
355         if (!request || !request->client) {
356                 *out = '\0';
357                 return 0;
358         }
359
360         cp = cf_pair_find(request->client->cs, fmt);
361         if (!cp || !(value = cf_pair_value(cp))) {
362                 *out = '\0';
363                 return 0;
364         }
365         
366         strlcpy(out, value, outlen);
367
368         return strlen(out);
369 }
370
371 /*
372  *      Recursively make directories.
373  */
374 static int r_mkdir(const char *part)
375 {
376         char *ptr, parentdir[500];
377         struct stat st;
378
379         if (stat(part, &st) == 0)
380                 return(0);
381
382         ptr = strrchr(part, FR_DIR_SEP);
383
384         if (ptr == part)
385                 return(0);
386
387         snprintf(parentdir, (ptr - part)+1, "%s", part);
388
389         if (r_mkdir(parentdir) != 0)
390                 return(1);
391
392         if (mkdir(part, 0770) != 0) {
393                 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
394                 return(1);
395         }
396
397         return(0);
398 }
399
400
401 /*
402  *  Do chroot, if requested.
403  *
404  *  Switch UID and GID to what is specified in the config file
405  */
406 static int switch_users(CONF_SECTION *cs)
407 {
408         int did_setuid = FALSE;
409         CONF_PAIR *cp;
410
411 #ifdef HAVE_SYS_RESOURCE_H
412         struct rlimit core_limits;
413 #endif
414
415         /*
416          *      Don't do chroot/setuid/setgid if we're in debugging
417          *      as non-root.
418          */
419         if (debug_flag && (getuid() != 0)) return 1;
420
421 #ifdef HAVE_GRP_H
422         /*  Set GID.  */
423         cp = cf_pair_find(cs, "group");
424         if (cp) gid_name = cf_pair_value(cp);
425         if (gid_name) {
426                 struct group *gr;
427
428                 DEBUG2("group = %s", gid_name);
429                 gr = getgrnam(gid_name);
430                 if (gr == NULL) {
431                         fprintf(stderr, "%s: Cannot get ID for group %s: %s\n",
432                                 progname, gid_name, strerror(errno));
433                         return 0;
434                 }
435                 server_gid = gr->gr_gid;
436         } else {
437                 server_gid = getgid();
438         }
439 #endif
440
441 #ifdef HAVE_PWD_H
442         /*  Set UID.  */
443         cp = cf_pair_find(cs, "user");
444         if (cp) uid_name = cf_pair_value(cp);
445         if (uid_name) {
446                 struct passwd *pw;
447                 
448                 DEBUG2("user = %s", uid_name);
449                 pw = getpwnam(uid_name);
450                 if (pw == NULL) {
451                         fprintf(stderr, "%s: Cannot get passwd entry for user %s: %s\n",
452                                 progname, uid_name, strerror(errno));
453                         return 0;
454                 }
455                 server_uid = pw->pw_uid;
456 #ifdef HAVE_INITGROUPS
457                 if (initgroups(uid_name, server_gid) < 0) {
458                         fprintf(stderr, "%s: Cannot initialize supplementary group list for user %s: %s\n",
459                                 progname, uid_name, strerror(errno));
460                         return 0;
461                 }
462 #endif
463         } else {
464                 server_uid = getuid();
465         }
466 #endif
467
468         cp = cf_pair_find(cs, "chroot");
469         if (cp) chroot_dir = cf_pair_value(cp);
470         if (chroot_dir) {
471                 DEBUG2("chroot = %s", chroot_dir);
472                 if (chroot(chroot_dir) < 0) {
473                         fprintf(stderr, "%s: Failed to perform chroot %s: %s",
474                                 progname, chroot_dir, strerror(errno));
475                         return 0;
476                 }
477
478                 /*
479                  *      Note that we leave chdir alone.  It may be
480                  *      OUTSIDE of the root.  This allows us to read
481                  *      the configuration from "-d ./etc/raddb", with
482                  *      the chroot as "./chroot/" for example.  After
483                  *      the server has been loaded, it does a "cd
484                  *      ${logdir}" below, so that core files (if any)
485                  *      go to a logging directory.
486                  *
487                  *      This also allows the configuration of the
488                  *      server to be outside of the chroot.  If the
489                  *      server is statically linked, then the only
490                  *      things needed inside of the chroot are the
491                  *      logging directories.
492                  */
493                 radlog(L_INFO, "performing chroot to %s\n", chroot_dir);
494         }
495
496 #ifdef HAVE_GRP_H
497         /*  Set GID.  */
498         if (gid_name && (setgid(server_gid) < 0)) {
499                 fprintf(stderr, "%s: Failed setting group to %s: %s",
500                         progname, gid_name, strerror(errno));
501                 return 0;
502         }
503 #endif
504
505 #ifdef HAVE_PWD_H
506         if (uid_name && (setuid(server_uid) < 0)) {
507                 fprintf(stderr, "%s: Failed setting user to %s: %s",
508                         progname, uid_name, strerror(errno));
509                 return 0;
510         }
511
512         /*
513          *      Now core dumps are disabled on most secure systems.
514          */
515         did_setuid = TRUE;
516 #endif
517
518         /*
519          *      Double check that we can write to the log directory.
520          *
521          *      If we can't, don't start, as we can't log any errors!
522          */
523         if ((mainconfig.radlog_dest == RADLOG_FILES) &&
524             (mainconfig.log_file != NULL)) {
525                 int fd = open(mainconfig.log_file,
526                               O_WRONLY | O_APPEND | O_CREAT, 0640);
527                 if (fd < 0) {
528                         fprintf(stderr, "%s: Cannot write to log file %s: %s\n",
529                                 progname, mainconfig.log_file, strerror(errno));
530                         return 0;
531                 }
532                 close(fd);
533
534                 /*
535                  *      After this it's safe to call radlog(), as it's going
536                  *      to the right place.
537                  */
538         }
539
540 #ifdef HAVE_SYS_RESOURCE_H
541         /*  Get the current maximum for core files.  */
542         if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
543                 radlog(L_ERR, "Failed to get current core limit:  %s", strerror(errno));
544                 return 0;
545         }
546 #endif
547
548         /*
549          *      Core dumps are allowed if we're in debug mode, OR
550          *      we've allowed them, OR we did a setuid (which turns
551          *      core dumps off).
552          *
553          *      Otherwise, disable core dumps for security.
554          *      
555          */
556         if (!(debug_flag || allow_core_dumps || did_setuid)) {
557 #ifdef HAVE_SYS_RESOURCE_H
558                 struct rlimit no_core;
559
560                 no_core.rlim_cur = 0;
561                 no_core.rlim_max = 0;
562
563                 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
564                         radlog(L_ERR, "Failed disabling core dumps: %s",
565                                strerror(errno));
566                         return 0;
567                 }
568 #endif
569
570                 /*
571                  *      Otherwise, re-enable core dumps if we're
572                  *      running as a daemon, AND core dumps are
573                  *      allowed, AND we changed UID's.
574                  */
575         } else if ((debug_flag == 0) && allow_core_dumps && did_setuid) {
576                 /*
577                  *      Set the dumpable flag.
578                  */
579 #ifdef HAVE_SYS_PRCTL_H
580 #ifdef PR_SET_DUMPABLE
581                 if (prctl(PR_SET_DUMPABLE, 1) < 0) {
582                         radlog(L_ERR,"Cannot enable core dumps: prctl(PR_SET_DUMPABLE) failed: '%s'",
583                                strerror(errno));
584                 }
585 #endif
586 #endif
587
588                 /*
589                  *      Reset the core dump limits again, just to
590                  *      double check that they haven't changed.
591                  */
592 #ifdef HAVE_SYS_RESOURCE_H
593                 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
594                         radlog(L_ERR, "Cannot update core dump limit: %s",
595                                         strerror(errno));
596                         return 0;
597                 }
598 #endif
599
600                 radlog(L_INFO, "Core dumps are enabled.");
601         }
602         /*
603          *      Else we're debugging (so core dumps are enabled)
604          *      OR we're not debugging, AND "allow_core_dumps == FALSE",
605          *      OR we're not debugging, AND core dumps are allowed,
606          *         BUT we didn't call setuid, so we haven't changed the
607          *         core dump capabilities inherited from the parent shell.
608          */
609
610         return 1;
611 }
612
613
614 static const FR_NAME_NUMBER str2dest[] = {
615         { "null", RADLOG_NULL },
616         { "files", RADLOG_FILES },
617         { "syslog", RADLOG_SYSLOG },
618         { "stdout", RADLOG_STDOUT },
619         { "stderr", RADLOG_STDERR },
620         { NULL, RADLOG_NUM_DEST }
621 };
622
623
624 /*
625  *      Read config files.
626  *
627  *      This function can ONLY be called from the main server process.
628  */
629 int read_mainconfig(int reload)
630 {
631         const char *p = NULL;
632         static int old_debug_level = -1;
633         CONF_PAIR *cp;
634         CONF_SECTION *cs;
635         struct stat statbuf;
636         char buffer[1024];
637
638         if (stat(radius_dir, &statbuf) < 0) {
639                 radlog(L_ERR, "Errors reading %s: %s",
640                        radius_dir, strerror(errno));
641                 return -1;
642         }
643
644 #ifdef S_IWOTH
645         if ((statbuf.st_mode & S_IWOTH) != 0) {
646                 radlog(L_ERR, "Configuration directory %s is globally writable.  Refusing to start due to insecure configuration.",
647                        radius_dir);
648           return -1;
649         }
650 #endif
651
652 #ifdef S_IROTH
653         if (0 && (statbuf.st_mode & S_IROTH) != 0) {
654                 radlog(L_ERR, "Configuration directory %s is globally readable.  Refusing to start due to insecure configuration.",
655                        radius_dir);
656                 return -1;
657         }
658 #endif
659
660         if (!reload) {
661                 radlog(L_INFO, "Starting - reading configuration files ...");
662         } else {
663                 radlog(L_INFO, "Reloading - reading configuration files...");
664         }
665
666         /* Read the configuration file */
667         snprintf(buffer, sizeof(buffer), "%.200s/%.50s.conf",
668                  radius_dir, mainconfig.name);
669         if ((cs = cf_file_read(buffer)) == NULL) {
670                 radlog(L_ERR, "Errors reading %s", buffer);
671                 return -1;
672         }
673
674         /*
675          *      If there was no log destination set on the command line,
676          *      set it now.
677          */
678         if (mainconfig.radlog_dest == RADLOG_NULL) {
679                 if (cf_section_parse(cs, NULL, serverdest_config) < 0) {
680                         fprintf(stderr, "radiusd: Error: Failed to parse log{} section.\n");
681                         cf_section_free(&cs);
682                         return -1;
683                 }
684                 
685                 if (!radlog_dest) {
686                         fprintf(stderr, "radiusd: Error: No log destination specified.\n");
687                         cf_section_free(&cs);
688                         return -1;
689                 }
690                 
691                 mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest,
692                                                     RADLOG_NUM_DEST);
693                 if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
694                         fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
695                                 radlog_dest);
696                         cf_section_free(&cs);
697                         return -1;
698                 }
699                 
700                 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
701                         /*
702                          *      Make sure syslog_facility isn't NULL
703                          *      before using it
704                          */
705                         if (!syslog_facility) {
706                                 fprintf(stderr, "radiusd: Error: Syslog chosen but no facility was specified\n");
707                                 cf_section_free(&cs);
708                                 return -1;
709                         }
710                         mainconfig.syslog_facility = fr_str2int(str2fac, syslog_facility, -1);
711                         if (mainconfig.syslog_facility < 0) {
712                                 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
713                                         syslog_facility);
714                                 cf_section_free(&cs);
715                                 return -1;
716                         }
717                 }
718         }
719
720         /*
721          *      We should really switch users earlier in the process.
722          */
723         if (!switch_users(cs)) exit(1);
724
725         /* Initialize the dictionary */
726         cp = cf_pair_find(cs, "dictionary");
727         if (cp) p = cf_pair_value(cp);
728         if (!p) p = radius_dir;
729         DEBUG2("including dictionary file %s/%s", p, RADIUS_DICTIONARY);
730         if (dict_init(p, RADIUS_DICTIONARY) != 0) {
731                 radlog(L_ERR, "Errors reading dictionary: %s",
732                                 librad_errstr);
733                 return -1;
734         }
735
736         /*
737          *      This allows us to figure out where, relative to
738          *      radiusd.conf, the other configuration files exist.
739          */
740         cf_section_parse(cs, NULL, server_config);
741
742         /*
743          *      Free the old configuration items, and replace them
744          *      with the new ones.
745          *
746          *      Note that where possible, we do atomic switch-overs,
747          *      to ensure that the pointers are always valid.
748          */
749         cf_section_free(&mainconfig.config);
750         mainconfig.config = cs;
751
752         clients_parse_section(cs);
753
754         DEBUG2("%s: #### Loading Realms and Home Servers ####", mainconfig.name);
755
756         if (!realms_init(cs)) {
757                 return -1;
758         }
759
760         /*
761          *  Register the %{config:section.subsection} xlat function.
762          */
763         xlat_register("config", xlat_config, NULL);
764         xlat_register("client", xlat_client, NULL);
765
766         /*
767          *      Reload: change debug flag if it's changed in the
768          *      configuration file.
769          */
770         if (reload) {
771                 if (mainconfig.debug_level != old_debug_level) {
772                         debug_flag = mainconfig.debug_level;
773                 }
774
775         } else if (debug_flag == 0) {
776
777                 /*
778                  *      Starting the server, WITHOUT "-x" on the
779                  *      command-line: use whatever's in the config
780                  *      file.
781                  */
782                 debug_flag = mainconfig.debug_level;
783         }
784         librad_debug = debug_flag;
785         old_debug_level = mainconfig.debug_level;
786
787         /*
788          *  Go update our behaviour, based on the configuration
789          *  changes.
790          */
791
792         /*
793          *      Sanity check the configuration for internal
794          *      consistency.
795          */
796         if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
797                 mainconfig.reject_delay = mainconfig.cleanup_delay;
798         }
799         if (mainconfig.reject_delay < 0) mainconfig.reject_delay = 0;
800
801         /*  Reload the modules.  */
802         if (setup_modules(reload, mainconfig.config) < 0) {
803                 radlog(L_ERR, "Errors initializing modules");
804                 return -1;
805         }
806
807         if (chroot_dir) {
808                 if (chdir(radlog_dir) < 0) {
809                         radlog(L_ERR, "Failed to 'chdir %s' after chroot: %s",
810                                radlog_dir, strerror(errno));
811                         return -1;
812                 }
813         }
814
815         return 0;
816 }
817
818 /*
819  *      Free the configuration.  Called only when the server is exiting.
820  */
821 int free_mainconfig(void)
822 {
823         /*
824          *      Clean up the configuration data
825          *      structures.
826          */
827         cf_section_free(&mainconfig.config);
828         realms_free();
829         listen_free(&mainconfig.listen);
830         dict_free();
831
832         return 0;
833 }