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