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