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