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