Separate pools and home servers by type.
[freeradius.git] / src / main / realms.c
1 /*
2  * realms.c     Realm handling code
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 2007  The FreeRADIUS server project
21  * Copyright 2007  Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
29
30 #include <sys/stat.h>
31
32 #include <ctype.h>
33 #include <fcntl.h>
34
35 static rbtree_t *realms_byname = NULL;
36
37 static rbtree_t *home_servers_byaddr = NULL;
38 static rbtree_t *home_servers_byname = NULL;
39
40 static rbtree_t *home_pools_byname = NULL;
41
42 static int realm_name_cmp(const void *one, const void *two)
43 {
44         const REALM *a = one;
45         const REALM *b = two;
46
47         return strcasecmp(a->name, b->name);
48 }
49
50
51 static int home_server_name_cmp(const void *one, const void *two)
52 {
53         const home_server *a = one;
54         const home_server *b = two;
55
56         if (a->type < b->type) return -1;
57         if (a->type > b->type) return +1;
58
59         return strcasecmp(a->name, b->name);
60 }
61
62 static int home_server_addr_cmp(const void *one, const void *two)
63 {
64         const home_server *a = one;
65         const home_server *b = two;
66
67         if (a->port < b->port) return -1;
68         if (a->port > b->port) return +1;
69
70         return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
71 }
72
73
74 static int home_pool_name_cmp(const void *one, const void *two)
75 {
76         const home_pool_t *a = one;
77         const home_pool_t *b = two;
78
79         if (a->server_type < b->server_type) return -1;
80         if (a->server_type > b->server_type) return +1;
81
82         return strcasecmp(a->name, b->name);
83 }
84
85
86 void realms_free(void)
87 {
88         rbtree_free(home_servers_byname);
89         home_servers_byname = NULL;
90
91         rbtree_free(home_servers_byaddr);
92         home_servers_byaddr = NULL;
93
94         rbtree_free(home_pools_byname);
95         home_pools_byname = NULL;
96
97         rbtree_free(realms_byname);
98         realms_byname = NULL;
99 }
100
101
102 int realms_init(const char *filename)
103 {
104         CONF_SECTION *cs;
105
106         if (realms_byname) return 1;
107
108         realms_byname = rbtree_create(realm_name_cmp, free, 0);
109         if (!realms_byname) {
110                 realms_free();
111                 return 0;
112         }
113
114         home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
115         if (!home_servers_byaddr) {
116                 realms_free();
117                 return 0;
118         }
119
120         home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
121         if (!home_servers_byname) {
122                 realms_free();
123                 return 0;
124         }
125
126         home_pools_byname = rbtree_create(home_pool_name_cmp, free, 0);
127         if (!home_pools_byname) {
128                 realms_free();
129                 return 0;
130         }
131
132         for (cs = cf_subsection_find_next(mainconfig.config, NULL, "realm");
133              cs != NULL;
134              cs = cf_subsection_find_next(mainconfig.config, cs, "realm")) {
135                 if (!realm_add(filename, cs)) {
136                         realms_free();
137                         return 0;
138                 }
139         }
140
141         return 1;
142 }
143
144 static struct in_addr hs_ip4addr;
145 static struct in6_addr hs_ip6addr;
146 static char *hs_type = NULL;
147 static char *hs_check = NULL;
148
149 static CONF_PARSER home_server_config[] = {
150         { "ipaddr",  PW_TYPE_IPADDR,
151           0, &hs_ip4addr,  NULL },
152         { "ipv6addr",  PW_TYPE_IPV6ADDR,
153           0, &hs_ip6addr, NULL },
154
155         { "hostname",  PW_TYPE_STRING_PTR,
156           offsetof(home_server,hostname), NULL,  NULL},
157         { "port", PW_TYPE_INTEGER,
158           offsetof(home_server,port), NULL,   "0" },
159
160         { "type",  PW_TYPE_STRING_PTR,
161           0, &hs_type, NULL },
162
163         { "secret",  PW_TYPE_STRING_PTR,
164           offsetof(home_server,secret), NULL,  NULL},
165
166         { "response_window", PW_TYPE_INTEGER,
167           offsetof(home_server,response_window), NULL,   "30" },
168         { "max_outstanding", PW_TYPE_INTEGER,
169           offsetof(home_server,max_outstanding), NULL,   "65536" },
170
171         { "zombie_period", PW_TYPE_INTEGER,
172           offsetof(home_server,zombie_period), NULL,   "40" },
173         { "status_check", PW_TYPE_STRING_PTR,
174           0, &hs_check,   "none" },
175         { "ping_check", PW_TYPE_STRING_PTR,
176           0, &hs_check,   "none" },
177
178         { "ping_interval", PW_TYPE_INTEGER,
179           offsetof(home_server,ping_interval), NULL,   "30" },
180         { "check_interval", PW_TYPE_INTEGER,
181           offsetof(home_server,ping_interval), NULL,   "30" },
182         { "num_answers_to_alive", PW_TYPE_INTEGER,
183           offsetof(home_server,num_pings_to_alive), NULL,   "3" },
184         { "num_pings_to_alive", PW_TYPE_INTEGER,
185           offsetof(home_server,num_pings_to_alive), NULL,   "3" },
186         { "revive_interval", PW_TYPE_INTEGER,
187           offsetof(home_server,revive_interval), NULL,   "300" },
188         { "status_check_timeout", PW_TYPE_INTEGER,
189           offsetof(home_server,ping_timeout), NULL,   "4" },
190
191         { "username",  PW_TYPE_STRING_PTR,
192           offsetof(home_server,ping_user_name), NULL,  NULL},
193         { "password",  PW_TYPE_STRING_PTR,
194           offsetof(home_server,ping_user_password), NULL,  NULL},
195
196         { NULL, -1, 0, NULL, NULL }             /* end the list */
197
198 };
199
200
201 static int home_server_add(const char *filename, CONF_SECTION *cs)
202 {
203         const char *name2;
204         home_server *home;
205
206         name2 = cf_section_name1(cs);
207         if (!name2 || (strcasecmp(name2, "home_server") != 0)) {
208                 radlog(L_ERR, "%s[%d]: Section is not a home_server.",
209                        filename, cf_section_lineno(cs));
210                 return 0;
211         }
212
213         name2 = cf_section_name2(cs);
214         if (!name2) {
215                 radlog(L_ERR, "%s[%d]: Home server section is missing a name.",
216                        filename, cf_section_lineno(cs));
217                 return 0;
218         }
219
220         home = rad_malloc(sizeof(*home));
221         memset(home, 0, sizeof(*home));
222
223         home->name = name2;
224
225         memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
226         memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
227         cf_section_parse(cs, home, home_server_config);
228
229         if (!home->hostname && (htonl(hs_ip4addr.s_addr) == INADDR_NONE) &&
230             IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
231                 radlog(L_ERR, "%s[%d]: No hostname, IPv4 address, or IPv6 address defined for home server %s.",
232                        filename, cf_section_lineno(cs), name2);
233                 free(home);
234                 free(hs_type);
235                 hs_type = NULL;
236                 free(hs_check);
237                 hs_check = NULL;
238                 return 0;
239         }
240
241         /*
242          *      FIXME: Parse home->hostname!
243          *
244          *      Right now, only ipaddr && ip6addr are used.
245          *      The old-style parsing still allows hostnames.
246          */
247         if (htonl(hs_ip4addr.s_addr) != INADDR_NONE) {
248                 home->ipaddr.af = AF_INET;
249                 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
250
251         } else if (!IN6_IS_ADDR_UNSPECIFIED(&hs_ip6addr)) {
252                 home->ipaddr.af = AF_INET6;
253                 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
254
255         } else {
256                 radlog(L_ERR, "%s[%d]: FIXME: parse hostname for home server %s.",
257                        filename, cf_section_lineno(cs), name2);
258                 free(home);
259                 free(hs_type);
260                 hs_type = NULL;
261                 free(hs_check);
262                 hs_check = NULL;
263                 return 0;
264         }
265
266         if (!home->port || (home->port > 65535)) {
267                 radlog(L_ERR, "%s[%d]: No port, or invalid port defined for home server %s.",
268                        filename, cf_section_lineno(cs), name2);
269                 free(home);
270                 free(hs_type);
271                 hs_type = NULL;
272                 free(hs_check);
273                 hs_check = NULL;
274                 return 0;
275         }
276
277         if (0) {
278                 radlog(L_ERR, "%s[%d]: Fatal error!  Home server %s is ourselves!",
279                        filename, cf_section_lineno(cs), name2);
280                 free(home);
281                 free(hs_type);
282                 hs_type = NULL;
283                 free(hs_check);
284                 hs_check = NULL;
285                 return 0;
286         }
287
288         if (strcasecmp(hs_type, "auth") == 0) {
289                 home->type = HOME_TYPE_AUTH;
290
291         } else if (strcasecmp(hs_type, "acct") == 0) {
292                 home->type = HOME_TYPE_ACCT;
293
294         } else {
295                 radlog(L_ERR, "%s[%d]: Invalid type \"%s\" for home server %s.",
296                        filename, cf_section_lineno(cs), hs_type, name2);
297                 free(home);
298                 free(hs_type);
299                 hs_type = NULL;
300                 free(hs_check);
301                 hs_check = NULL;
302                 return 0;
303         }
304         free(hs_type);
305         hs_type = NULL;
306
307         if (!home->secret) {
308                 radlog(L_ERR, "%s[%d]: No shared secret defined for home server %s.",
309                        filename, cf_section_lineno(cs), name2);
310                 free(home);
311                 return 0;
312         }
313
314         if (strcasecmp(hs_check, "none") == 0) {
315                 home->ping_check = HOME_PING_CHECK_NONE;
316
317         } else if (strcasecmp(hs_check, "status-server") == 0) {
318                 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
319
320         } else if (strcasecmp(hs_check, "request") == 0) {
321                 home->ping_check = HOME_PING_CHECK_REQUEST;
322
323         } else {
324                 radlog(L_ERR, "%s[%d]: Invalid ping_check \"%s\" for home server %s.",
325                        filename, cf_section_lineno(cs), hs_check, name2);
326                 free(home);
327                 free(hs_check);
328                 hs_check = NULL;
329                 return 0;
330         }
331         free(hs_check);
332         hs_check = NULL;
333
334         if ((home->ping_check != HOME_PING_CHECK_NONE) &&
335             (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
336                 if (!home->ping_user_name) {
337                         radlog(L_INFO, "%s[%d]: You must supply a user name to enable ping checks",
338                                filename, cf_section_lineno(cs));
339                         free(home);
340                         return 0;
341                 }
342
343                 if ((home->type == HOME_TYPE_AUTH) &&
344                     !home->ping_user_password) {
345                         radlog(L_INFO, "%s[%d]: You must supply a password to enable ping checks",
346                                filename, cf_section_lineno(cs));
347                         free(home);
348                         return 0;
349                 }
350         }
351
352         if (rbtree_finddata(home_servers_byaddr, home)) {
353                 radlog(L_INFO, "%s[%d]: Ignoring duplicate home server %s.",
354                        filename, cf_section_lineno(cs), name2);
355                 return 1;
356         }
357
358         if (!rbtree_insert(home_servers_byname, home)) {
359                 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
360                        filename, cf_section_lineno(cs), name2);
361                 free(home);
362                 return 0;
363         }
364
365         if (!rbtree_insert(home_servers_byaddr, home)) {
366                 rbtree_deletebydata(home_servers_byname, home);
367                 radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
368                        filename, cf_section_lineno(cs), name2);
369                 free(home);
370                 return 0;
371         }
372
373         if (home->response_window < 5) home->response_window = 5;
374         if (home->response_window > 60) home->response_window = 60;
375
376         if (home->max_outstanding < 8) home->max_outstanding = 8;
377         if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
378
379         if (home->ping_interval < 6) home->ping_interval = 6;
380         if (home->ping_interval > 120) home->ping_interval = 120;
381
382         if (home->zombie_period < 20) home->zombie_period = 20;
383         if (home->zombie_period > 120) home->zombie_period = 120;
384
385         if (home->zombie_period < home->response_window) {
386                 home->zombie_period = home->response_window;
387         }
388
389         if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
390         if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
391
392         if (home->ping_timeout < 3) home->ping_timeout = 3;
393         if (home->ping_timeout > 10) home->ping_timeout = 10;
394
395         if (home->revive_interval < 60) home->revive_interval = 60;
396         if (home->revive_interval > 3600) home->revive_interval = 3600;
397
398         return 1;
399 }
400
401
402 static int server_pool_add(const char *filename, CONF_SECTION *cs, int type)
403 {
404         const char *name2;
405         home_pool_t *pool = NULL;
406         const char *value;
407         CONF_PAIR *cp;
408         int num_home_servers;
409
410         name2 = cf_section_name1(cs);
411         if (!name2 || (strcasecmp(name2, "server_pool") != 0)) {
412                 radlog(L_ERR, "%s[%d]: Section is not a server_pool.",
413                        filename, cf_section_lineno(cs));
414                 return 0;
415         }
416
417         name2 = cf_section_name2(cs);
418         if (!name2) {
419                 radlog(L_ERR, "%s[%d]: Server pool section is missing a name.",
420                        filename, cf_section_lineno(cs));
421                 return 0;
422         }
423
424         /*
425          *      Count the home servers and initalize them.
426          */
427         num_home_servers = 0;
428         for (cp = cf_pair_find(cs, "home_server");
429              cp != NULL;
430              cp = cf_pair_find_next(cs, cp, "home_server")) {
431                 home_server myhome, *home;
432                 CONF_SECTION *server_cs;
433
434                 num_home_servers++;
435
436                 value = cf_pair_value(cp);
437                 if (!value) {
438                         radlog(L_ERR, "%s[%d]: No value given for home_server.",
439                                filename, cf_pair_lineno(cp));
440                         return 0;;
441                 }
442
443                 myhome.name = value;
444                 myhome.type = type;
445                 home = rbtree_finddata(home_servers_byname, &myhome);
446                 if (home) continue;
447
448                 server_cs = cf_section_sub_find_name2(NULL,
449                                                       "home_server",
450                                                       value);
451                 if (!server_cs) {
452                         radlog(L_ERR, "%s[%d]: Unknown home_server \"%s\".",
453                                filename, cf_pair_lineno(cp), value);
454                         return 0;
455                 }
456
457                 if (!home_server_add(filename, server_cs)) {
458                         return 0;
459                 }
460
461                 home = rbtree_finddata(home_servers_byname, &myhome);
462                 if (!home) {
463                         radlog(L_ERR, "Internal sanity check failed %d",
464                                __LINE__);
465                         return 0;
466                 }
467         }
468
469         if (num_home_servers == 0) {
470                 radlog(L_ERR, "%s[%d]: No home servers defined in pool %s",
471                        filename, cf_section_lineno(cs), name2);
472                 goto error;
473         }
474
475         pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
476         memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
477
478         pool->type = HOME_POOL_FAIL_OVER;
479         pool->name = name2;
480
481         DEBUG2(" server_pool %s {", name2);
482
483         cp = cf_pair_find(cs, "type");
484         if (cp) {
485                 static LRAD_NAME_NUMBER pool_types[] = {
486                         { "load-balance", HOME_POOL_LOAD_BALANCE },
487                         { "fail-over", HOME_POOL_FAIL_OVER },
488                         { "round_robin", HOME_POOL_LOAD_BALANCE },
489                         { "fail_over", HOME_POOL_FAIL_OVER },
490                         { "client-balance", HOME_POOL_CLIENT_BALANCE },
491                         { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
492                         { NULL, 0 }
493                 };
494
495                 value = cf_pair_value(cp);
496                 if (!value) {
497                         radlog(L_ERR, "%s[%d]: No value given for type.",
498                                filename, cf_pair_lineno(cp));
499                         goto error;
500                 }
501
502                 pool->type = lrad_str2int(pool_types, value, 0);
503                 if (!pool->type) {
504                         radlog(L_ERR, "%s[%d]: Unknown type \"%s\".",
505                                filename, cf_pair_lineno(cp), value);
506                         goto error;
507                 }
508
509                 DEBUG2("\ttype = %s", name2, value);
510         }
511
512         for (cp = cf_pair_find(cs, "home_server");
513              cp != NULL;
514              cp = cf_pair_find_next(cs, cp, "home_server")) {
515                 home_server myhome, *home;
516
517                 value = cf_pair_value(cp);
518                 if (!value) {
519                         radlog(L_ERR, "%s[%d]: No value given for home_server.",
520                                filename, cf_pair_lineno(cp));
521                         goto error;
522                 }
523
524                 myhome.name = value;
525                 myhome.type = type;
526
527                 home = rbtree_finddata(home_servers_byname, &myhome);
528                 if (!home) {
529                         DEBUG2("Internal sanity check failed");
530                         goto error;
531                 }
532
533                 if (!pool->server_type) {
534                         rad_assert(home->type != 0);
535                         pool->server_type = home->type;
536
537                 } else if (pool->server_type != home->type) {
538                         radlog(L_ERR, "%s[%d]: Home server \"%s\" is not of the same type as previous servers in server pool %s",
539                                filename, cf_pair_lineno(cp), value, pool->name);
540                         goto error;
541                 }
542
543                 if (0) {
544                         DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
545                         continue;
546                 }
547
548                 DEBUG2("\thome_server = %s", home->name);
549                 pool->servers[pool->num_home_servers] = home;
550                 pool->num_home_servers++;
551         } /* loop over home_server's */
552
553         if (!rbtree_insert(home_pools_byname, pool)) {
554                 rad_assert("Internal sanity check failed");
555                 goto error;
556         }
557
558         DEBUG2(" }");
559
560         rad_assert(pool->server_type != 0);
561
562         return 1;
563
564  error:
565         DEBUG2(" }");
566         free(pool);
567         return 0;
568 }
569
570
571 static int old_server_add(const char *filename, int lineno, const char *realm,
572                           const char *name, const char *secret,
573                           home_pool_type_t ldflag, home_pool_t **pool_p,
574                           int type)
575 {
576         int i, insert_point, num_home_servers;
577         home_server myhome, *home;
578         home_pool_t mypool, *pool;
579         CONF_SECTION *cs;
580
581         /*
582          *      LOCAL realms get sanity checked, and nothing else happens.
583          */
584         if (strcmp(name, "LOCAL") == 0) {
585                 if (*pool_p) {
586                         radlog(L_ERR, "%s[%d]: Realm \"%s\" cannot be both LOCAL and remote", filename, lineno, name);
587                         return 0;
588                 }
589                 return 1;
590         }
591
592         mypool.name = realm;
593         mypool.server_type = type;
594         pool = rbtree_finddata(home_pools_byname, &mypool);
595         if (pool) {
596                 if (pool->type != ldflag) {
597                         radlog(L_ERR, "%s[%d]: Inconsistent ldflag for server pool \"%s\"", filename, lineno, name);
598                         return 0;
599                 }
600
601                 if (pool->server_type != type) {
602                         radlog(L_ERR, "%s[%d]: Inconsistent home server type for server pool \"%s\"", filename, lineno, name);
603                         return 0;
604                 }
605         }
606
607         myhome.name = name;
608         home = rbtree_finddata(home_servers_byname, &myhome);
609         if (home) {
610                 if (strcmp(home->secret, secret) != 0) {
611                         radlog(L_ERR, "%s[%d]: Inconsistent shared secret for home server \"%s\"", filename, lineno, name);
612                         return 0;
613                 }
614
615                 if (home->type != type) {
616                         radlog(L_ERR, "%s[%d]: Inconsistent type for home server \"%s\"", filename, lineno, name);
617                         return 0;
618                 }
619
620                 /*
621                  *      See if the home server is already listed
622                  *      in the pool.  If so, do nothing else.
623                  */
624                 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
625                         if (pool->servers[i] == home) {
626                                 return 1;
627                         }
628                 }
629         }
630
631         /*
632          *      If we do have a pool, check that there is room to
633          *      insert the home server we've found, or the one that we
634          *      create here.
635          *
636          *      Note that we insert it into the LAST available
637          *      position, in order to maintain the same order as in
638          *      the configuration files.
639          */
640         insert_point = -1;
641         if (pool) {
642                 for (i = pool->num_home_servers - 1; i >= 0; i--) {
643                         if (pool->servers[i]) break;
644
645                         if (!pool->servers[i]) {
646                                 insert_point = i;
647                         }
648                 }
649
650                 if (insert_point < 0) {
651                         radlog(L_ERR, "%s[%d]: No room in pool to add home server \"%s\".  Please update the realm configuration to use the new-style home servers and server pools.", filename, lineno, name);
652                         return 0;
653                 }
654         }
655
656         /*
657          *      No home server, allocate one.
658          */
659         if (!home) {
660                 const char *p;
661                 char *q;
662
663                 home = rad_malloc(sizeof(*home));
664                 memset(home, 0, sizeof(*home));
665
666                 home->name = name;
667                 home->hostname = name;
668                 home->type = type;
669                 home->secret = secret;
670
671                 p = strchr(name, ':');
672                 if (!p) {
673                         if (type == HOME_TYPE_AUTH) {
674                                 home->port = PW_AUTH_UDP_PORT;
675                         } else {
676                                 home->port = PW_ACCT_UDP_PORT;
677                         }
678
679                         p = name;
680                         q = NULL;
681
682                 } else if (p == name) {
683                                 radlog(L_ERR, "%s[%d]: Invalid hostname %s.",
684                                        filename, lineno, name);
685                                 free(home);
686                                 return 0;
687
688                 } else {
689                         home->port = atoi(p + 1);
690                         if ((home->port == 0) || (home->port > 65535)) {
691                                 radlog(L_ERR, "%s[%d]: Invalid port %s.",
692                                        filename, lineno, p + 1);
693                                 free(home);
694                                 return 0;
695                         }
696
697                         q = rad_malloc((p - name) + 1);
698                         memcpy(q, name, (p - name));
699                         q[p - name] = '\0';
700                         p = q;
701                 }
702
703                 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
704                         radlog(L_ERR, "%s[%d]: Failed looking up hostname %s.",
705                                filename, lineno, p);
706                         free(home);
707                         free(q);
708                         return 0;
709                 }
710                 free(q);
711
712                 /*
713                  *      Use the old-style configuration.
714                  */
715                 home->max_outstanding = 65535*16;
716                 home->zombie_period = mainconfig.proxy_retry_delay * mainconfig.proxy_retry_count;
717                 if (home->zombie_period == 0) home->zombie_period =30;
718                 home->response_window = home->zombie_period - 1;
719
720                 home->ping_check = HOME_PING_CHECK_NONE;
721
722                 home->revive_interval = mainconfig.proxy_dead_time;
723
724                 if (rbtree_finddata(home_servers_byaddr, home)) {
725                         radlog(L_ERR, "%s[%d]: Home server %s has the same IP address as another home server.",
726                                filename, lineno, name);
727                         free(home);
728                         return 0;
729                 }
730
731                 if (!rbtree_insert(home_servers_byname, home)) {
732                         radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
733                                filename, lineno, name);
734                         free(home);
735                         return 0;
736                 }
737
738                 if (!rbtree_insert(home_servers_byaddr, home)) {
739                         rbtree_deletebydata(home_servers_byname, home);
740                         radlog(L_ERR, "%s[%d]: Internal error adding home server %s.",
741                                filename, lineno, name);
742                         free(home);
743                         return 0;
744                 }
745         }
746
747         /*
748          *      We now have a home server, see if we can insert it
749          *      into pre-existing pool.
750          */
751         if (insert_point >= 0) {
752                 rad_assert(pool != NULL);
753                 pool->servers[insert_point] = home;
754                 return 1;
755         }
756
757         rad_assert(pool == NULL);
758         rad_assert(home != NULL);
759
760         /*
761          *      Count the old-style realms of this name.
762          */
763         num_home_servers = 0;
764         for (cs = cf_section_sub_find_name2(mainconfig.config, "realm", realm);
765              cs != NULL;
766              cs = cf_section_sub_find_name2(cs, "realm", realm)) {
767                 num_home_servers++;
768         }
769
770         pool = rad_malloc(sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
771         memset(pool, 0, sizeof(*pool) + num_home_servers * sizeof(pool->servers[0]));
772
773         pool->name = realm;
774         pool->type = ldflag;
775         pool->server_type = type;
776         pool->num_home_servers = num_home_servers;
777         pool->servers[0] = home;
778
779         if (!rbtree_insert(home_pools_byname, pool)) {
780                 rad_assert("Internal sanity check failed");
781                 return 0;
782         }
783
784         *pool_p = pool;
785
786         return 1;
787 }
788
789 static int old_realm_config(const char *filename, CONF_SECTION *cs, REALM *r)
790 {
791         char *host;
792         const char *secret;
793         home_pool_type_t ldflag;
794
795         secret = cf_section_value_find(cs, "secret");
796
797         host = cf_section_value_find(cs, "ldflag");
798         if (!host ||
799             (strcasecmp(host, "fail_over") == 0)) {
800                 ldflag = HOME_POOL_FAIL_OVER;
801                 DEBUG2("\tldflag = fail_over");
802
803         } else if (strcasecmp(host, "round_robin") == 0) {
804                 ldflag = HOME_POOL_LOAD_BALANCE;
805                 DEBUG2("\tldflag = round_robin");
806
807         } else {
808                 radlog(L_ERR, "%s[%d]: Unknown value \"%s\" for ldflag",
809                        filename, cf_section_lineno(cs), host);
810                 return 0;
811         }
812
813         /*
814          *      Allow old-style if it doesn't exist, or if it exists and
815          *      it's LOCAL.
816          */
817         if (((host = cf_section_value_find(cs, "authhost")) != NULL) &&
818             (strcmp(host, "LOCAL") != 0)) {
819                 if (!secret) {
820                         radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
821                                filename, cf_section_lineno(cs), r->name);
822                         return 0;
823                 }
824
825                 DEBUG2("\tauthhost = %s",  host);
826
827                 if (!old_server_add(filename, cf_section_lineno(cs),
828                                     r->name, host, secret, ldflag,
829                                     &r->auth_pool, HOME_TYPE_AUTH)) {
830                         return 0;
831                 }
832         }
833
834         if (((host = cf_section_value_find(cs, "accthost")) != NULL) &&
835             (strcmp(host, "LOCAL") != 0)) {
836                 if (!secret) {
837                         radlog(L_ERR, "%s[%d]: No shared secret supplied for realm: %s",
838                                filename, cf_section_lineno(cs), r->name);
839                         return 0;
840                 }
841
842                 DEBUG2("\taccthost = %s", host);
843
844                 if (!old_server_add(filename, cf_section_lineno(cs),
845                                     r->name, host, secret, ldflag,
846                                     &r->acct_pool, HOME_TYPE_ACCT)) {
847                         return 0;
848                 }
849         }
850
851         if (secret) DEBUG2("\tsecret = %s", secret);
852
853         return 1;
854
855 }
856
857
858 static int add_pool_to_realm(const char *filename, int lineno,
859                              const char *name, home_pool_t **dest,
860                              int server_type)
861 {
862         home_pool_t mypool, *pool;
863
864         mypool.name = name;
865         mypool.server_type = server_type;
866         pool = rbtree_finddata(home_pools_byname, &mypool);
867         if (!pool) {
868                 CONF_SECTION *pool_cs;
869
870                 pool_cs = cf_section_sub_find_name2(NULL, "server_pool",
871                                                     name);
872                 if (!pool_cs) {
873                         radlog(L_ERR, "%s[%d]: Failed to find server_pool \"%s\"",
874                                filename, lineno, name);
875                         return 0;
876                 }
877
878                 if (!server_pool_add(filename, pool_cs, server_type)) {
879                         return 0;
880                 }
881
882                 pool = rbtree_finddata(home_pools_byname, &mypool);
883                 if (!pool) {
884                         radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
885                         return 0;
886                 }
887         }
888
889         if (pool->server_type != server_type) {
890                 radlog(L_ERR, "%s[%d]: Incompatible server_pool \"%s\" (mixed auth_pool / acct_pool)",
891                        filename, lineno, name);
892                 return 0;
893         }
894
895         *dest = pool;
896
897         return 1;
898 }
899
900 int realm_add(const char *filename, CONF_SECTION *cs)
901 {
902         const char *name2;
903         REALM *r = NULL;
904         CONF_PAIR *cp;
905         home_pool_t *auth_pool, *acct_pool;
906         const char *auth_pool_name, *acct_pool_name;
907
908         name2 = cf_section_name1(cs);
909         if (!name2 || (strcasecmp(name2, "realm") != 0)) {
910                 radlog(L_ERR, "%s[%d]: Section is not a realm.",
911                        filename, cf_section_lineno(cs));
912                 return 0;
913         }
914
915         name2 = cf_section_name2(cs);
916         if (!name2) {
917                 radlog(L_ERR, "%s[%d]: Realm section is missing the realm name.",
918                        filename, cf_section_lineno(cs));
919                 return 0;
920         }
921
922         auth_pool = acct_pool = NULL;
923         auth_pool_name = acct_pool_name = NULL;
924
925         /*
926          *      Prefer new configuration to old one.
927          */
928         cp = cf_pair_find(cs, "auth_pool");
929         if (cp) auth_pool_name = cf_pair_value(cp);
930         if (cp && auth_pool_name) {
931                 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
932                                        auth_pool_name, &auth_pool,
933                                        HOME_TYPE_AUTH)) {
934                         return 0;
935                 }
936         }
937
938         cp = cf_pair_find(cs, "acct_pool");
939         if (cp) acct_pool_name = cf_pair_value(cp);
940         if (cp && acct_pool_name) {
941                 if (!add_pool_to_realm(filename, cf_pair_lineno(cp),
942                                        acct_pool_name, &acct_pool,
943                                        HOME_TYPE_ACCT)) {
944                         return 0;
945                 }
946         }
947
948         DEBUG2(" realm %s {", name2);
949
950         /*
951          *      The realm MAY already exist if it's an old-style realm.
952          *      In that case, merge the old-style realm with this one.
953          */
954         r = realm_find(name2);
955         if (r) {
956                 if (cf_pair_find(cs, "auth_pool") ||
957                     cf_pair_find(cs, "acct_pool")) {
958                         radlog(L_ERR, "%s[%d]: Duplicate realm \"%s\"",
959                                filename, cf_section_lineno(cs), name2);
960                         goto error;
961                 }
962
963                 if (!old_realm_config(filename, cs, r)) {
964                         goto error;
965                 }
966
967                 DEBUG2(" } # realm %s", name2);
968                 return 1;
969         }
970
971         r = rad_malloc(sizeof(*r));
972         memset(r, 0, sizeof(*r));
973
974         r->name = name2;
975         r->auth_pool = auth_pool;
976         r->acct_pool = acct_pool;
977         r->striprealm = 1;
978
979         if (auth_pool_name) DEBUG2("\tauth_pool = %s", auth_pool_name);
980         if (acct_pool_name) DEBUG2("\tacct_pool = %s", acct_pool_name);
981
982         if ((cf_section_value_find(cs, "nostrip")) != NULL) {
983                 r->striprealm = 0;
984                 DEBUG2("\tnostrip", name2);
985         }
986
987         /*
988          *      We're a new-style realm.  Complain if we see the old
989          *      directives.
990          */
991         if (r->auth_pool || r->acct_pool) {
992                 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
993                     ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
994                     ((cp = cf_pair_find(cs, "secret")) != NULL) ||
995                     ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
996                         DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
997                 }
998
999
1000                 /*
1001                  *      The realm MAY be an old-style realm, as there
1002                  *      was no auth_pool or acct_pool.  Double-check
1003                  *      it, just to be safe.
1004                  */
1005         } else if (!old_realm_config(filename, cs, r)) {
1006                 goto error;
1007         }
1008
1009         if (!rbtree_insert(realms_byname, r)) {
1010                 rad_assert("Internal sanity check failed");
1011                 goto error;
1012         }
1013
1014         DEBUG2(" }");
1015
1016         return 1;
1017
1018  error:
1019         DEBUG2(" } # realm %s", name2);
1020         free(r);
1021         return 0;
1022 }
1023
1024
1025 /*
1026  *      Find a realm in the REALM list.
1027  */
1028 REALM *realm_find(const char *name)
1029 {
1030         REALM myrealm;
1031
1032         if (!name) name = "NULL";
1033
1034         myrealm.name = name;
1035         return rbtree_finddata(realms_byname, &myrealm);
1036 }
1037
1038
1039 home_server *home_server_ldb(const char *realmname,
1040                              home_pool_t *pool, REQUEST *request)
1041 {
1042         int             start;
1043         int             count;
1044         home_server     *found = NULL;
1045
1046         start = 0;
1047
1048         /*
1049          *      Determine how to pick choose the home server.
1050          */
1051         switch (pool->type) {
1052                 uint32_t hash;
1053
1054                 /*
1055                  *      For load-balancing by client IP address, we
1056                  *      pick a home server by hashing the client IP.
1057                  *
1058                  *      This isn't as even a load distribution as
1059                  *      tracking the State attribute, but it's better
1060                  *      than nothing.
1061                  */
1062         case HOME_POOL_CLIENT_BALANCE:
1063                 switch (request->packet->src_ipaddr.af) {
1064                 case AF_INET:
1065                         hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1066                                          sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1067                         break;
1068                 case AF_INET6:
1069                         hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1070                                          sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1071                         break;
1072                 default:
1073                         hash = 0;
1074                         break;
1075                 }
1076                 start = hash % pool->num_home_servers;
1077                 break;
1078
1079         case HOME_POOL_CLIENT_PORT_BALANCE:
1080                 switch (request->packet->src_ipaddr.af) {
1081                 case AF_INET:
1082                         hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1083                                          sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1084                         break;
1085                 case AF_INET6:
1086                         hash = lrad_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1087                                          sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1088                         break;
1089                 default:
1090                         hash = 0;
1091                         break;
1092                 }
1093                 lrad_hash_update(&request->packet->src_port,
1094                                  sizeof(request->packet->src_port), hash);
1095                 start = hash % pool->num_home_servers;
1096                 break;
1097
1098         case HOME_POOL_LOAD_BALANCE:
1099                 found = pool->servers[0];
1100
1101         default:
1102                 start = 0;
1103                 break;
1104         }
1105
1106         /*
1107          *      Starting with the home server we chose, loop through
1108          *      all home servers.  If the current one is dead, skip
1109          *      it.  If it is too busy, skip it.
1110          *
1111          *      Otherwise, use it.
1112          */
1113         for (count = 0; count < pool->num_home_servers; count++) {
1114                 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1115
1116                 if (home->state == HOME_STATE_IS_DEAD) {
1117                         continue;
1118                 }
1119
1120                 /*
1121                  *      This home server is too busy.  Choose another one.
1122                  */
1123                 if (home->currently_outstanding >= home->max_outstanding) {
1124                         continue;
1125                 }
1126
1127                 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1128                         return home;
1129                 }
1130
1131                 DEBUG3("PROXY %s %d\t%s %d",
1132                        found->name, found->currently_outstanding,
1133                        home->name, home->currently_outstanding);
1134
1135                 /*
1136                  *      Prefer this server if it's less busy than the
1137                  *      one we previously found.
1138                  */
1139                 if (home->currently_outstanding < found->currently_outstanding) {
1140                         DEBUG3("Choosing %s: It's less busy than %s",
1141                                home->name, found->name);
1142                         found = home;
1143                         continue;
1144                 }
1145
1146                 /*
1147                  *      Ignore servers which are busier than the one
1148                  *      we found.
1149                  */
1150                 if (home->currently_outstanding > found->currently_outstanding) {
1151                         DEBUG3("Skipping %s: It's busier than %s",
1152                                home->name, found->name);
1153                         continue;
1154                 }
1155
1156                 /*
1157                  *      From the list of servers which have the same
1158                  *      load, choose one at random.
1159                  */
1160                 if (((count + 1) * (lrad_rand() & 0xffff)) < (uint32_t) 0x10000) {
1161                         found = home;
1162                 }
1163
1164         } /* loop over the home servers */
1165
1166         if (found) return found;
1167
1168         /*
1169          *      No live match found, and no fallback to the "DEFAULT"
1170          *      realm.  We fix this by blindly marking all servers as
1171          *      "live".  But only do it for ones that don't support
1172          *      "pings", as they will be marked live when they
1173          *      actually are live.
1174          */
1175         if (!mainconfig.proxy_fallback &&
1176             mainconfig.wake_all_if_all_dead) {
1177                 home_server *lb = NULL;
1178
1179                 for (count = 0; count < pool->num_home_servers; count++) {
1180                         home_server *home = pool->servers[count];
1181
1182                         if ((home->state == HOME_STATE_IS_DEAD) &&
1183                             (home->ping_check == HOME_PING_CHECK_NONE)) {
1184                                 home->state = HOME_STATE_ALIVE;
1185                                 if (!lb) lb = home;
1186                         }
1187                 }
1188
1189                 if (lb) return lb;
1190         }
1191
1192         /*
1193          *      Still nothing.  Look up the DEFAULT realm, but only
1194          *      if we weren't looking up the NULL or DEFAULT realms.
1195          */
1196         if (mainconfig.proxy_fallback &&
1197             realmname &&
1198             (strcmp(realmname, "NULL") != 0) &&
1199             (strcmp(realmname, "DEFAULT") != 0)) {
1200                 REALM *rd = realm_find("DEFAULT");
1201
1202                 if (!rd) return NULL;
1203
1204                 pool = NULL;
1205                 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
1206                         pool = rd->auth_pool;
1207
1208                 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
1209                         pool = rd->acct_pool;
1210                 }
1211                 if (!pool) return NULL;
1212
1213                 DEBUG2("  Realm %s has no live home servers.  Falling back to the DEFAULT realm.", realmname);
1214                 return home_server_ldb(rd->name, pool, request);
1215         }
1216
1217         /*
1218          *      Still haven't found anything.  Oh well.
1219          */
1220         return NULL;
1221 }
1222
1223
1224 home_server *home_server_find(lrad_ipaddr_t *ipaddr, int port)
1225 {
1226         home_server myhome;
1227
1228         myhome.ipaddr = *ipaddr;
1229         myhome.port = port;
1230
1231         return rbtree_finddata(home_servers_byaddr, &myhome);
1232 }