992d43caafe4894e76c8878804295cf924074d2c
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2002  The FreeRADIUS server project
21  * Copyright 2002  Alan DeKok <aland@ox.org>
22  */
23
24 #include "autoconf.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36
37 #include "radiusd.h"
38 #include "rad_assert.h"
39 #include "conffile.h"
40 #include "token.h"
41
42 #include <sys/resource.h>
43 #include <unistd.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netdb.h>
47 #include <sys/stat.h>
48 #include <grp.h>
49 #include <pwd.h>
50
51
52 #ifdef HAVE_SYSLOG_H
53 #       include <syslog.h>
54 #endif
55
56 struct main_config_t mainconfig;
57
58 /*
59  *      Temporary local variables for parsing the configuration
60  *      file.
61  */
62 static uid_t server_uid;
63 static gid_t server_gid;
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 LRAD_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  *  Map the proxy server configuration parameters to variables.
134  */
135 static const CONF_PARSER proxy_config[] = {
136         { "retry_delay",  PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_delay, Stringify(RETRY_DELAY) },
137         { "retry_count",  PW_TYPE_INTEGER, 0, &mainconfig.proxy_retry_count, Stringify(RETRY_COUNT) },
138         { "default_fallback", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_fallback, "no" },
139         { "dead_time",    PW_TYPE_INTEGER, 0, &mainconfig.proxy_dead_time, Stringify(DEAD_TIME) },
140         { "wake_all_if_all_dead", PW_TYPE_BOOLEAN, 0, &mainconfig.wake_all_if_all_dead, "no" },
141         { "proxy_fail_type", PW_TYPE_STRING_PTR, 0, &mainconfig.proxy_fail_type, NULL},
142         { NULL, -1, 0, NULL, NULL }
143 };
144
145
146 /*
147  *  Security configuration for the server.
148  */
149 static const CONF_PARSER security_config[] = {
150         { "max_attributes",  PW_TYPE_INTEGER, 0, &librad_max_attributes, Stringify(0) },
151         { "reject_delay",  PW_TYPE_INTEGER, 0, &mainconfig.reject_delay, Stringify(0) },
152         { "status_server", PW_TYPE_BOOLEAN, 0, &mainconfig.status_server, "no"},
153         { NULL, -1, 0, NULL, NULL }
154 };
155
156
157 /*
158  *  syslog configuration for the server.
159  */
160 static const CONF_PARSER log_config[] = {
161         { "syslog_facility",  PW_TYPE_STRING_PTR, 0, &syslog_facility, Stringify(0) },
162         { NULL, -1, 0, NULL, NULL }
163 };
164
165
166 /*
167  *  A mapping of configuration file names to internal variables
168  */
169 static const CONF_PARSER server_config[] = {
170         /*
171          *      FIXME: 'prefix' is the ONLY one which should be
172          *      configured at compile time.  Hard-coding it here is
173          *      bad.  It will be cleaned up once we clean up the
174          *      hard-coded defines for the locations of the various
175          *      files.
176          */
177         { "prefix",             PW_TYPE_STRING_PTR, 0, &prefix,            "/usr/local"},
178         { "localstatedir",      PW_TYPE_STRING_PTR, 0, &localstatedir,     "${prefix}/var"},
179         { "logdir",             PW_TYPE_STRING_PTR, 0, &radlog_dir,        "${localstatedir}/log"},
180         { "libdir",             PW_TYPE_STRING_PTR, 0, &radlib_dir,        "${prefix}/lib"},
181         { "radacctdir",         PW_TYPE_STRING_PTR, 0, &radacct_dir,       "${logdir}/radacct" },
182         { "hostname_lookups",   PW_TYPE_BOOLEAN,    0, &librad_dodns,      "no" },
183 #ifdef WITH_SNMP
184         { "snmp",               PW_TYPE_BOOLEAN,    0, &mainconfig.do_snmp,      "no" },
185 #endif
186         { "max_request_time", PW_TYPE_INTEGER, 0, &mainconfig.max_request_time, Stringify(MAX_REQUEST_TIME) },
187         { "cleanup_delay", PW_TYPE_INTEGER, 0, &mainconfig.cleanup_delay, Stringify(CLEANUP_DELAY) },
188         { "max_requests", PW_TYPE_INTEGER, 0, &mainconfig.max_requests, Stringify(MAX_REQUESTS) },
189         { "delete_blocked_requests", PW_TYPE_INTEGER, 0, &mainconfig.kill_unresponsive_children, Stringify(FALSE) },
190         { "allow_core_dumps", PW_TYPE_BOOLEAN, 0, &mainconfig.allow_core_dumps, "no" },
191         { "log_stripped_names", PW_TYPE_BOOLEAN, 0, &log_stripped_names,"no" },
192
193         { "log_file", PW_TYPE_STRING_PTR, -1, &mainconfig.log_file, "${logdir}/radius.log" },
194         { "log_auth", PW_TYPE_BOOLEAN, -1, &mainconfig.log_auth, "no" },
195         { "log_auth_badpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_badpass, "no" },
196         { "log_auth_goodpass", PW_TYPE_BOOLEAN, 0, &mainconfig.log_auth_goodpass, "no" },
197         { "pidfile", PW_TYPE_STRING_PTR, 0, &mainconfig.pid_file, "${run_dir}/radiusd.pid"},
198         { "user", PW_TYPE_STRING_PTR, 0, &mainconfig.uid_name, NULL},
199         { "group", PW_TYPE_STRING_PTR, 0, &mainconfig.gid_name, NULL},
200         { "checkrad", PW_TYPE_STRING_PTR, 0, &mainconfig.checkrad, "${sbindir}/checkrad" },
201
202         { "debug_level", PW_TYPE_INTEGER, 0, &mainconfig.debug_level, "0"},
203
204         { "proxy_requests", PW_TYPE_BOOLEAN, 0, &mainconfig.proxy_requests, "yes" },
205         { "log", PW_TYPE_SUBSECTION, 0, log_config, NULL },
206         { "proxy", PW_TYPE_SUBSECTION, 0, proxy_config, NULL },
207         { "security", PW_TYPE_SUBSECTION, 0, security_config, NULL },
208         { NULL, -1, 0, NULL, NULL }
209 };
210
211
212 #define MAX_ARGV (256)
213 /*
214  *      Xlat for %{config:section.subsection.attribute}
215  */
216 static int xlat_config(void *instance, REQUEST *request,
217                        char *fmt, char *out,
218                        size_t outlen,
219                        RADIUS_ESCAPE_STRING func)
220 {
221         CONF_SECTION *cs;
222         CONF_PAIR *cp;
223         int i, argc, left;
224         const char *from, *value;
225         char *to;
226         char myfmt[1024];
227         char argv_buf[1024];
228         char *argv[MAX_ARGV];
229
230         request = request;      /* -Wunused */
231         instance = instance;    /* -Wunused */
232
233         cp = NULL;
234         cs = NULL;
235
236         /*
237          *      Split the string into argv's BEFORE doing radius_xlat...
238          *      Copied from exec.c
239          */
240         from = fmt;
241         to = myfmt; 
242         argc = 0;
243         while (*from) {
244                 int flag, length;
245                 
246                 flag = 0;
247                 argv[argc] = to;
248                 argc++;
249                 
250                 if (argc >= (MAX_ARGV - 1)) break;
251                 
252                 /*
253                  *      Copy the argv over to our buffer.
254                  */
255                 while (*from) {
256                         if (to >= myfmt + sizeof(myfmt) - 1) {
257                                 return 0; /* no error msg */
258                         }
259
260                         switch (*from) {
261                         case '%':
262                                 if (from[1] == '{') {
263                                         *(to++) = *(from++);
264                                         
265                                         length = rad_copy_variable(to, from);
266                                         if (length < 0) {
267                                                 return -1;
268                                         }
269                                         from += length;
270                                         to += length;
271                                 } else { /* FIXME: catch %%{ ? */
272                                         *(to++) = *(from++);
273                                 }
274                                 break;
275
276                         case '[':
277                                 if (flag != 0) {
278                                         radlog(L_ERR, "config: Unexpected nested '[' in \"%s\"", fmt);
279                                         return 0;
280                                 }
281                                 flag++;
282                                 *(to++) = *(from++);
283                                 break;
284
285                         case ']':
286                                 if (flag == 0) {
287                                         radlog(L_ERR, "config: Unbalanced ']' in \"%s\"", fmt);
288                                         return 0;
289                                 }
290                                 if (from[1] != '.') {
291                                         radlog(L_ERR, "config: Unexpected text after ']' in \"%s\"", fmt);
292                                         return 0;
293                                 }
294
295                                 flag--;
296                                 *(to++) = *(from++);
297                                 break;
298
299                         case '.':
300                                 if (flag == 0) break;
301                                 /* FALL-THROUGH */
302
303                         default:
304                                 *(to++) = *(from++);
305                                 break;
306                         }
307
308                         if ((*from == '.') && (flag == 0)) {
309                                 from++;
310                                 break;
311                         }
312                 } /* end of string, or found a period */
313
314                 if (flag != 0) {
315                         radlog(L_ERR, "config: Unbalanced '[' in \"%s\"", fmt);
316                         return 0;
317                 }
318
319                 *(to++) = '\0'; /* terminate the string. */
320         }
321
322         /*
323          *      Expand each string, as appropriate
324          */
325         to = argv_buf;
326         left = sizeof(argv_buf);
327         for (i = 0; i < argc; i++) {
328                 int sublen;
329
330                 /*
331                  *      Don't touch argv's which won't be translated.
332                  */
333                 if (strchr(argv[i], '%') == NULL) continue;
334
335                 sublen = radius_xlat(to, left - 1, argv[i], request, NULL);
336                 if (sublen <= 0) {
337                         /*
338                          *      Fail to be backwards compatible.
339                          *
340                          *      It's yucky, but it won't break anything,
341                          *      and it won't cause security problems.
342                          */
343                         sublen = 0;
344                 }
345                 
346                 argv[i] = to;
347                 to += sublen;
348                 *(to++) = '\0';
349                 left -= sublen;
350                 left--;
351
352                 if (left <= 0) {
353                         return 0;
354                 }
355         }
356         argv[argc] = NULL;
357
358         cs = cf_section_find(NULL); /* get top-level section */
359
360         /*
361          *      Root through section & subsection references.
362          *      The last entry of argv MUST be the CONF_PAIR.
363          */
364         for (i = 0; i < argc - 1; i++) {
365                 char *name2 = NULL;
366                 CONF_SECTION *subcs;
367
368                 /*
369                  *      FIXME: What about RADIUS attributes containing '['?
370                  */
371                 name2 = strchr(argv[i], '[');
372                 if (name2) {
373                         char *p = strchr(name2, ']');
374                         rad_assert(p != NULL);
375                         rad_assert(p[1] =='\0');
376                         *p = '\0';
377                         *name2 = '\0';
378                         name2++;
379                 }
380
381                 if (name2) {
382                         subcs = cf_section_sub_find_name2(cs, argv[i],
383                                                           name2);
384                         if (!subcs) {
385                           radlog(L_ERR, "config: section \"%s %s {}\" not found while dereferencing \"%s\"", argv[i], name2, fmt);
386                           return 0;
387                         }
388                 } else {
389                         subcs = cf_section_sub_find(cs, argv[i]);
390                         if (!subcs) {
391                           radlog(L_ERR, "config: section \"%s {}\" not found while dereferencing \"%s\"", argv[i], fmt);
392                           return 0;
393                         }
394                 }
395                 cs = subcs;
396         } /* until argc - 1 */
397
398         /*
399          *      This can now have embedded periods in it.
400          */
401         cp = cf_pair_find(cs, argv[argc - 1]);
402         if (!cp) {
403                 radlog(L_ERR, "config: item \"%s\" not found while dereferencing \"%s\"", argv[argc], fmt);
404                 return 0;
405         }
406
407         /*
408          *  Ensure that we only copy what's necessary.
409          *
410          *  If 'outlen' is too small, then the output is chopped to fit.
411          */
412         value = cf_pair_value(cp);
413         if (value) {
414                 if (outlen > strlen(value)) {
415                         outlen = strlen(value) + 1;
416                 }
417         }
418
419         return func(out, outlen, value);
420 }
421
422
423 /*
424  *      Recursively make directories.
425  */
426 static int r_mkdir(const char *part)
427 {
428         char *ptr, parentdir[500];
429         struct stat st;
430
431         if (stat(part, &st) == 0)
432                 return(0);
433
434         ptr = strrchr(part, '/');
435
436         if (ptr == part)
437                 return(0);
438
439         snprintf(parentdir, (ptr - part)+1, "%s", part);
440
441         if (r_mkdir(parentdir) != 0)
442                 return(1);
443
444         if (mkdir(part, 0770) != 0) {
445                 radlog(L_ERR, "mkdir(%s) error: %s\n", part, strerror(errno));
446                 return(1);
447         }
448
449         return(0);
450 }
451
452 /*
453  *      Checks if the log directory is writeable by a particular user.
454  */
455 static int radlogdir_iswritable(const char *effectiveuser)
456 {
457         struct passwd *pwent;
458
459         if (radlog_dir[0] != '/')
460                 return(0);
461
462         if (r_mkdir(radlog_dir) != 0)
463                 return(1);
464
465         /* FIXME: do we have this function? */
466         if (strstr(radlog_dir, "radius") == NULL)
467                 return(0);
468
469         /* we have a logdir that mentions 'radius', so it's probably
470          * safe to chown the immediate directory to be owned by the normal
471          * process owner. we gotta do it before we give up root.  -chad
472          */
473
474         if (!effectiveuser) {
475                 return 1;
476         }
477
478         pwent = getpwnam(effectiveuser);
479
480         if (pwent == NULL) /* uh oh! */
481                 return(1);
482
483         if (chown(radlog_dir, pwent->pw_uid, -1) != 0)
484                 return(1);
485
486         return(0);
487 }
488
489
490 /*
491  *  Switch UID and GID to what is specified in the config file
492  */
493 static int switch_users(void)
494 {
495         /*  Set GID.  */
496         if (mainconfig.gid_name != NULL) {
497                 struct group *gr;
498
499                 gr = getgrnam(mainconfig.gid_name);
500                 if (gr == NULL) {
501                         if (errno == ENOMEM) {
502                                 radlog(L_ERR|L_CONS, "Cannot switch to Group %s: out of memory", mainconfig.gid_name);
503                         } else {
504                                 radlog(L_ERR|L_CONS, "Cannot switch group; %s doesn't exist", mainconfig.gid_name);
505                         }
506                         exit(1);
507                 }
508                 server_gid = gr->gr_gid;
509                 if (setgid(server_gid) < 0) {
510                         radlog(L_ERR|L_CONS, "Failed setting Group to %s: %s",
511                                mainconfig.gid_name, strerror(errno));
512                         exit(1);
513                 }
514         } else {
515                 server_gid = getgid();
516         }
517
518         /*  Set UID.  */
519         if (mainconfig.uid_name != NULL) {
520                 struct passwd *pw;
521
522                 pw = getpwnam(mainconfig.uid_name);
523                 if (pw == NULL) {
524                         if (errno == ENOMEM) {
525                                 radlog(L_ERR|L_CONS, "Cannot switch to User %s: out of memory", mainconfig.uid_name);
526                         } else {
527                                 radlog(L_ERR|L_CONS, "Cannot switch user; %s doesn't exist", mainconfig.uid_name);
528                         }
529                         exit(1);
530                 }
531                 server_uid = pw->pw_uid;
532 #ifdef HAVE_INITGROUPS
533                 if (initgroups(mainconfig.uid_name, server_gid) < 0) {
534                         if (errno != EPERM) {
535                                 radlog(L_ERR|L_CONS, "Failed setting supplementary groups for User %s: %s", mainconfig.uid_name, strerror(errno));
536                                 exit(1);
537                         }
538                 }
539 #endif
540                 if (setuid(server_uid) < 0) {
541                         radlog(L_ERR|L_CONS, "Failed setting User to %s: %s", mainconfig.uid_name, strerror(errno));
542                         exit(1);
543                 }
544         }
545
546         /*
547          *      We've probably written to the log file already as
548          *      root.root, so if we have switched users, we've got to
549          *      update the ownership of the file.
550          */
551         if ((debug_flag == 0) &&
552             (mainconfig.radlog_dest == RADLOG_FILES) &&
553             (mainconfig.log_file != NULL)) {
554                 chown(mainconfig.log_file, server_uid, server_gid);
555         }
556         return(0);
557 }
558
559
560 /*
561  * Create the linked list of realms from the new configuration type
562  * This way we don't have to change to much in the other source-files
563  */
564 static int generate_realms(const char *filename)
565 {
566         CONF_SECTION *cs;
567         REALM *my_realms = NULL;
568         REALM *c, **tail;
569         char *s, *t, *authhost, *accthost;
570         const char *name2;
571
572         tail = &my_realms;
573         for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
574              cs != NULL;
575              cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
576                 name2 = cf_section_name2(cs);
577                 if (!name2) {
578                         radlog(L_CONS|L_ERR, "%s[%d]: Missing realm name",
579                                filename, cf_section_lineno(cs));
580                         return -1;
581                 }
582                 /*
583                  * We've found a realm, allocate space for it
584                  */
585                 c = rad_malloc(sizeof(REALM));
586                 memset(c, 0, sizeof(REALM));
587
588                 c->secret[0] = '\0';
589
590                 /*
591                  *      No authhost means LOCAL.
592                  */
593                 if ((authhost = cf_section_value_find(cs, "authhost")) == NULL) {
594                         c->ipaddr.af = AF_INET;
595                         c->ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
596                         c->auth_port = 0;
597                 } else {
598                         if ((s = strchr(authhost, ':')) != NULL) {
599                                 *s++ = 0;
600                                 c->auth_port = atoi(s);
601                         } else {
602                                 c->auth_port = PW_AUTH_UDP_PORT;
603                         }
604                         if (strcmp(authhost, "LOCAL") == 0) {
605                                 /*
606                                  *      Local realms don't have an IP address,
607                                  *      secret, or port.
608                                  */
609                                 c->ipaddr.af = AF_INET;
610                                 c->ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
611                                 c->auth_port = 0;
612                         } else {
613                                 if (ip_hton(authhost, AF_INET,
614                                             &c->ipaddr) < 0) {
615                                         radlog(L_ERR, "%s[%d]: Host %s not found",
616                                                filename, cf_section_lineno(cs),
617                                                authhost);
618                                         return -1;
619                                 }
620                         }
621
622                         /*
623                          * Double check length, just to be sure!
624                          */
625                         if (strlen(authhost) >= sizeof(c->server)) {
626                                 radlog(L_ERR, "%s[%d]: Server name of length %d is greater than allowed: %d",
627                                        filename, cf_section_lineno(cs),
628                                        (int) strlen(authhost),
629                                        (int) sizeof(c->server) - 1);
630                                 return -1;
631                         }
632                 }
633
634                 /*
635                  *      No accthost means LOCAL
636                  */
637                 if ((accthost = cf_section_value_find(cs, "accthost")) == NULL) {
638                         c->acct_ipaddr.af = AF_INET;
639                         c->acct_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
640                         c->acct_port = 0;
641                 } else {
642                         if ((s = strchr(accthost, ':')) != NULL) {
643                                 *s++ = 0;
644                                 c->acct_port = atoi(s);
645                         } else {
646                                 c->acct_port = PW_ACCT_UDP_PORT;
647                         }
648                         if (strcmp(accthost, "LOCAL") == 0) {
649                                 /*
650                                  *      Local realms don't have an IP address,
651                                  *      secret, or port.
652                                  */
653                                 c->acct_ipaddr.af = AF_INET;
654                                 c->acct_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
655                                 c->acct_port = 0;
656                         } else {
657                                 if (ip_hton(accthost, AF_INET,
658                                             &c->acct_ipaddr) < 0) {
659                                         radlog(L_ERR, "%s[%d]: Host %s not found",
660                                                filename, cf_section_lineno(cs),
661                                                accthost);
662                                         return -1;
663                                 }
664                         }
665
666                         if (strlen(accthost) >= sizeof(c->acct_server)) {
667                                 radlog(L_ERR, "%s[%d]: Server name of length %d is greater than allowed: %d",
668                                        filename, cf_section_lineno(cs),
669                                        (int) strlen(accthost),
670                                        (int) sizeof(c->acct_server) - 1);
671                                 return -1;
672                         }
673                 }
674
675                 if (strlen(name2) >= sizeof(c->realm)) {
676                         radlog(L_ERR, "%s[%d]: Realm name of length %d is greater than allowed %d",
677                                filename, cf_section_lineno(cs),
678                                (int) strlen(name2),
679                                (int) sizeof(c->server) - 1);
680                         return -1;
681                 }
682
683                 strcpy(c->realm, name2);
684                 if (authhost) strcpy(c->server, authhost);
685                 if (accthost) strcpy(c->acct_server, accthost);
686
687                 /*
688                  *      If one or the other of authentication/accounting
689                  *      servers is set to LOCALHOST, then don't require
690                  *      a shared secret.
691                  */
692                 rad_assert(c->ipaddr.af == AF_INET);
693                 rad_assert(c->acct_ipaddr.af == AF_INET);
694                 if ((c->ipaddr.ipaddr.ip4addr.s_addr != htonl(INADDR_NONE)) ||
695                     (c->acct_ipaddr.ipaddr.ip4addr.s_addr != htonl(INADDR_NONE))) {
696                         if ((s = cf_section_value_find(cs, "secret")) == NULL ) {
697                                 radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
698                                        filename, cf_section_lineno(cs), name2);
699                                 return -1;
700                         }
701
702                         if (strlen(s) >= sizeof(c->secret)) {
703                                 radlog(L_ERR, "%s[%d]: Secret of length %d is greater than the allowed maximum of %d.",
704                                        filename, cf_section_lineno(cs),
705                                        strlen(s), sizeof(c->secret) - 1);
706                                 return -1;
707                         }
708                         strNcpy((char *)c->secret, s, sizeof(c->secret));
709                 }
710
711                 c->striprealm = 1;
712
713                 if ((cf_section_value_find(cs, "nostrip")) != NULL)
714                         c->striprealm = 0;
715                 if ((cf_section_value_find(cs, "noacct")) != NULL)
716                         c->acct_port = 0;
717                 if ((cf_section_value_find(cs, "trusted")) != NULL)
718                         c->trusted = 1;
719                 if ((cf_section_value_find(cs, "notrealm")) != NULL)
720                         c->notrealm = 1;
721                 if ((cf_section_value_find(cs, "notsuffix")) != NULL)
722                         c->notrealm = 1;
723                 if ((t = cf_section_value_find(cs,"ldflag")) != NULL) {
724                         static const LRAD_NAME_NUMBER ldflags[] = {
725                                 { "fail_over",   0 },
726                                 { "round_robin", 1 },
727                                 { NULL, 0 }
728                         };
729
730                         c->ldflag = lrad_str2int(ldflags, t, -1);
731                         if (c->ldflag == -1) {
732                                 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
733                                        filename, cf_section_lineno(cs),
734                                        t);
735                                 return -1;
736                         }
737
738                 } else {
739                         c->ldflag = 0; /* non, make it fail-over */
740                 }
741                 c->active = TRUE;
742                 c->acct_active = TRUE;
743
744                 c->next = NULL;
745                 *tail = c;
746                 tail = &c->next;
747         }
748
749         /*
750          *      And make these realms preferred over the ones
751          *      in the 'realms' file.
752          */
753         *tail = mainconfig.realms;
754         mainconfig.realms = my_realms;
755
756         /*
757          *  Ensure that all of the flags agree for the realms.
758          *
759          *      Yeah, it's O(N^2), but it's only once, and the
760          *      maximum number of realms is small.
761          */
762         for(c = mainconfig.realms; c != NULL; c = c->next) {
763                 REALM *this;
764
765                 /*
766                  *      Check that we cannot load balance to LOCAL
767                  *      realms, as that doesn't make any sense.
768                  */
769                 rad_assert(c->ipaddr.af == AF_INET);
770                 rad_assert(c->acct_ipaddr.af == AF_INET);
771                 if ((c->ldflag == 1) &&
772                     ((c->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_NONE)) ||
773                      (c->acct_ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_NONE)))) {
774                         radlog(L_ERR | L_CONS, "ERROR: Realm %s cannot be load balanced to LOCAL",
775                                c->realm);
776                         exit(1);
777                 }
778
779                 /*
780                  *      Compare this realm to all others, to ensure
781                  *      that the configuration is consistent.
782                  */
783                 for (this = c->next; this != NULL; this = this->next) {
784                         if (strcasecmp(c->realm, this->realm) != 0) {
785                                 continue;
786                         }
787
788                         /*
789                          *      Same realm: Different load balancing
790                          *      flag: die.
791                          */
792                         if (c->ldflag != this->ldflag) {
793                                 radlog(L_ERR | L_CONS, "ERROR: Inconsistent value in realm %s for load balancing 'ldflag' attribute",
794                                        c->realm);
795                                 exit(1);
796                         }
797                 }
798         }
799
800         return 0;
801 }
802
803
804 static const LRAD_NAME_NUMBER str2dest[] = {
805         { "files", RADLOG_FILES },
806         { "syslog", RADLOG_SYSLOG },
807         { "stdout", RADLOG_STDOUT },
808         { "stderr", RADLOG_STDERR },
809         { NULL, RADLOG_NULL }
810 };
811
812
813 /*
814  *      Read config files.
815  *
816  *      This function can ONLY be called from the main server process.
817  */
818 int read_mainconfig(int reload)
819 {
820         struct rlimit core_limits;
821         static int old_debug_level = -1;
822         char buffer[1024];
823         CONF_SECTION *cs, *oldcs, *newcs;
824         rad_listen_t *listener;
825
826         if (!reload) {
827                 radlog(L_INFO, "Starting - reading configuration files ...");
828         } else {
829                 radlog(L_INFO, "Reloading configuration files.");
830         }
831
832         /* Initialize the dictionary */
833         DEBUG2("read_config_files:  reading dictionary");
834         if (dict_init(radius_dir, RADIUS_DICTIONARY) != 0) {
835                 radlog(L_ERR|L_CONS, "Errors reading dictionary: %s",
836                                 librad_errstr);
837                 cf_section_free(&cs);
838                 return -1;
839         }
840
841         /* Read the configuration file */
842         snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
843                  radius_dir, mainconfig.radiusd_conf);
844         if ((cs = conf_read(NULL, 0, buffer, NULL)) == NULL) {
845                 radlog(L_ERR|L_CONS, "Errors reading %s", buffer);
846                 return -1;
847         }
848
849         /*
850          *      This allows us to figure out where, relative to
851          *      radiusd.conf, the other configuration files exist.
852          */
853         cf_section_parse(cs, NULL, server_config);
854
855 #if 0
856         /*
857          *      Merge the old with the new.
858          */
859         if (reload) {
860                 newcs = cf_section_sub_find(cs, "modules");
861                 oldcs = cf_section_sub_find(mainconfig.config, "modules");
862                 if (newcs && oldcs) {
863                         if (!cf_section_migrate(newcs, oldcs)) {
864                                 radlog(L_ERR|L_CONS, "Fatal error migrating configuration data");
865                                 return -1;
866                         }
867                 }
868         }
869 #endif
870
871         /*
872          *      Debug flag 1 MAY go to files.
873          *      Debug flag 2 ALWAYS goes to stdout
874          */
875         if (debug_flag < 2) {
876                 int rcode;
877                 char *radlog_dest = NULL;
878
879                 rcode = cf_item_parse(cs, "log_destination",
880                                       PW_TYPE_STRING_PTR, &radlog_dest,
881                                       "files");
882                 if (rcode < 0) return NULL;
883         
884                 mainconfig.radlog_dest = lrad_str2int(str2dest, radlog_dest, RADLOG_NULL);
885                 if (mainconfig.radlog_dest == RADLOG_NULL) {
886                         fprintf(stderr, "radiusd: Error: Unknown log_destination %s\n",
887                                 radlog_dest);
888                         cf_section_free(&cs);
889                         return NULL;
890                 }
891                 
892                 if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
893                         mainconfig.syslog_facility = lrad_str2int(str2fac, syslog_facility, -1);
894                         if (mainconfig.syslog_facility < 0) {
895                                 fprintf(stderr, "radiusd: Error: Unknown syslog_facility %s\n",
896                                         syslog_facility);
897                                 cf_section_free(&cs);
898                                 return NULL;
899                         }
900                 }
901         }
902
903         /*
904          *      Free the old configuration items, and replace them
905          *      with the new ones.
906          *
907          *      Note that where possible, we do atomic switch-overs,
908          *      to ensure that the pointers are always valid.
909          */
910         oldcs = mainconfig.config;
911         mainconfig.config = cs;
912         cf_section_free(&oldcs);
913
914         /*
915          *      Old-style realms file.
916          */
917         snprintf(buffer, sizeof(buffer), "%.200s/%.50s", radius_dir, RADIUS_REALMS);
918         DEBUG2("read_config_files:  reading realms");
919         if (read_realms_file(buffer) < 0) {
920                 radlog(L_ERR|L_CONS, "Errors reading realms");
921                 return -1;
922         }
923
924         /*
925          *      If there isn't any realms it isn't fatal..
926          */
927         snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
928                  radius_dir, mainconfig.radiusd_conf);
929         if (generate_realms(buffer) < 0) {
930                 return -1;
931         }
932
933         /*
934          *  Register the %{config:section.subsection} xlat function.
935          */
936         xlat_register("config", xlat_config, NULL);
937
938         /*
939          *      Set the libraries debugging flag to whatever the main
940          *      flag is.  Note that on a SIGHUP, to turn the debugging
941          *      off, we do other magic.
942          *
943          *      Increase the debug level, if the configuration file
944          *      says to, OR, if we're decreasing the debug from what it
945          *      was before, allow that, too.
946          */
947         if ((mainconfig.debug_level > debug_flag) ||
948             (mainconfig.debug_level <= old_debug_level)) {
949                 debug_flag = mainconfig.debug_level;
950         }
951         librad_debug = debug_flag;
952         old_debug_level = mainconfig.debug_level;
953
954         /*
955          *  Go update our behaviour, based on the configuration
956          *  changes.
957          */
958
959         /*  Get the current maximum for core files.  */
960         if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
961                 radlog(L_ERR|L_CONS, "Failed to get current core limit:  %s", strerror(errno));
962                 exit(1);
963         }
964
965         if (mainconfig.allow_core_dumps) {
966                 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
967                         radlog(L_ERR|L_CONS, "Cannot update core dump limit: %s",
968                                         strerror(errno));
969                         exit(1);
970
971                         /*
972                          *  If we're running as a daemon, and core
973                          *  dumps are enabled, log that information.
974                          */
975                 } else if ((core_limits.rlim_cur != 0) && !debug_flag)
976                         radlog(L_INFO|L_CONS, "Core dumps are enabled.");
977
978         } else if (!debug_flag) {
979                 /*
980                  *  Not debugging.  Set the core size to zero, to
981                  *  prevent security breaches.  i.e. People
982                  *  reading passwords from the 'core' file.
983                  */
984                 struct rlimit limits;
985
986                 limits.rlim_cur = 0;
987                 limits.rlim_max = core_limits.rlim_max;
988
989                 if (setrlimit(RLIMIT_CORE, &limits) < 0) {
990                         radlog(L_ERR|L_CONS, "Cannot disable core dumps: %s",
991                                         strerror(errno));
992                         exit(1);
993                 }
994         }
995
996         /*
997          *      The first time around, ensure that we can write to the
998          *      log directory.
999          */
1000         if (!reload) {
1001                 /*
1002                  *      We need root to do mkdir() and chown(), so we
1003                  *      do this before giving up root.
1004                  */
1005                 radlogdir_iswritable(mainconfig.uid_name);
1006         }
1007
1008         /*
1009          *      We should really switch users earlier in the process.
1010          */
1011         switch_users();
1012
1013         /*
1014          *      Sanity check the configuration for internal
1015          *      consistency.
1016          */
1017         if (mainconfig.reject_delay > mainconfig.cleanup_delay) {
1018                 mainconfig.reject_delay = mainconfig.cleanup_delay;
1019         }
1020
1021         /*
1022          *      Initialize the old "bind_address" and "port", first.
1023          */
1024         listener = NULL;
1025
1026         /*
1027          *      Read the list of listeners.
1028          */
1029         snprintf(buffer, sizeof(buffer), "%.200s/%s", radius_dir, mainconfig.radiusd_conf);
1030         if (listen_init(buffer, &listener) < 0) {
1031                 exit(1);
1032         }
1033
1034         if (!listener) {
1035                 radlog(L_ERR|L_CONS, "Server is not configured to listen on any ports.  Exiting.");
1036                 exit(1);
1037         }
1038
1039         listen_free(&mainconfig.listen);
1040         mainconfig.listen = listener;
1041
1042         /*
1043          *      Walk through the listeners.  If we're listening on acct
1044          *      or auth, read in the clients files, else ignore them.
1045          */
1046         for (listener = mainconfig.listen;
1047              listener != NULL;
1048              listener = listener->next) {
1049                 if ((listener->type == RAD_LISTEN_AUTH) ||
1050                     (listener->type == RAD_LISTEN_ACCT)) {
1051                         break;
1052                 }
1053         }
1054
1055         if (listener != NULL) {
1056                 RADCLIENT_LIST *clients, *old_clients;
1057
1058                 /* old-style naslist file */
1059                 snprintf(buffer, sizeof(buffer), "%.200s/%.50s", radius_dir, RADIUS_NASLIST);
1060                 DEBUG2("read_config_files:  reading naslist");
1061                 if (read_naslist_file(buffer) < 0) {
1062                         radlog(L_ERR|L_CONS, "Errors reading naslist");
1063                         return -1;
1064                 }
1065
1066                 /*
1067                  *      Create the new clients first, and add them
1068                  *      to the CONF_SECTION, where they're automagically
1069                  *      freed if anything goes wrong.
1070                  */
1071                 snprintf(buffer, sizeof(buffer), "%.200s/%.50s",
1072                          radius_dir, mainconfig.radiusd_conf);
1073                 clients = clients_parse_section(buffer, mainconfig.config);
1074                 if (!clients) {
1075                         return -1;
1076                 }
1077
1078                 /*
1079                  *      Free the old trees AFTER replacing them with
1080                  *      the new ones...
1081                  */
1082                 old_clients = mainconfig.clients;
1083                 mainconfig.clients = clients;
1084         }
1085
1086         /*  Reload the modules.  */
1087         DEBUG2("radiusd:  entering modules setup");
1088         if (setup_modules(reload) < 0) {
1089                 radlog(L_ERR|L_CONS, "Errors setting up modules");
1090                 return -1;
1091         }
1092         return 0;
1093 }
1094
1095 /*
1096  *      Free the configuration.
1097  */
1098 int free_mainconfig(void)
1099 {
1100         /*
1101          *      Clean up the configuration data
1102          *      structures.
1103          */
1104         cf_section_free(&mainconfig.config);
1105         realm_free(mainconfig.realms);
1106         listen_free(&mainconfig.listen);
1107
1108         return 0;
1109 }