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