2 * realms.c Realm handling code
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.
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.
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
20 * Copyright 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
35 #ifdef HAVE_PCREPOSIX_H
36 #include <pcreposix.h>
42 * For POSIX Regular expressions.
43 * (0) Means no extended regular expressions.
44 * REG_EXTENDED means use extended regular expressions.
47 #define REG_EXTENDED (0)
60 static rbtree_t *realms_byname = NULL;
63 typedef struct realm_regex_t {
65 struct realm_regex_t *next;
68 static realm_regex_t *realms_regex = NULL;
70 #endif /* HAVE_REGEX_H */
72 typedef struct realm_config_t {
78 int wake_all_if_all_dead;
81 static realm_config_t *realm_config = NULL;
84 static rbtree_t *home_servers_byaddr = NULL;
85 static rbtree_t *home_servers_byname = NULL;
87 static int home_server_max_number = 0;
88 static rbtree_t *home_servers_bynumber = NULL;
91 static rbtree_t *home_pools_byname = NULL;
94 * Map the proxy server configuration parameters to variables.
96 static const CONF_PARSER proxy_config[] = {
97 { "retry_delay", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_delay),
99 NULL, Stringify(RETRY_DELAY) },
101 { "retry_count", PW_TYPE_INTEGER,
102 offsetof(realm_config_t, retry_count),
103 NULL, Stringify(RETRY_COUNT) },
105 { "default_fallback", PW_TYPE_BOOLEAN,
106 offsetof(realm_config_t, fallback),
109 { "dead_time", PW_TYPE_INTEGER,
110 offsetof(realm_config_t, dead_time),
111 NULL, Stringify(DEAD_TIME) },
113 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
114 offsetof(realm_config_t, wake_all_if_all_dead),
117 { NULL, -1, 0, NULL, NULL }
121 static int realm_name_cmp(const void *one, const void *two)
123 const REALM *a = one;
124 const REALM *b = two;
126 return strcasecmp(a->name, b->name);
131 static void home_server_free(void *data)
133 home_server *home = data;
136 tls_server_conf_free(home->tls);
143 static int home_server_name_cmp(const void *one, const void *two)
145 const home_server *a = one;
146 const home_server *b = two;
148 if (a->type < b->type) return -1;
149 if (a->type > b->type) return +1;
151 return strcasecmp(a->name, b->name);
154 static int home_server_addr_cmp(const void *one, const void *two)
157 const home_server *a = one;
158 const home_server *b = two;
160 if (a->server && !b->server) return -1;
161 if (!a->server && b->server) return +1;
162 if (a->server && b->server) {
163 rcode = a->type - b->type;
164 if (rcode != 0) return rcode;
165 return strcmp(a->server, b->server);
168 if (a->port < b->port) return -1;
169 if (a->port > b->port) return +1;
172 if (a->proto < b->proto) return -1;
173 if (a->proto > b->proto) return +1;
176 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
177 if (rcode != 0) return rcode;
179 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
183 static int home_server_number_cmp(const void *one, const void *two)
185 const home_server *a = one;
186 const home_server *b = two;
188 return (a->number - b->number);
192 static int home_pool_name_cmp(const void *one, const void *two)
194 const home_pool_t *a = one;
195 const home_pool_t *b = two;
197 if (a->server_type < b->server_type) return -1;
198 if (a->server_type > b->server_type) return +1;
200 return strcasecmp(a->name, b->name);
204 static size_t xlat_cs(CONF_SECTION *cs, char *fmt, char *out, size_t outlen)
207 const char *value = NULL;
212 if (strcmp(fmt, "instance") == 0) {
213 value = cf_section_name2(cs);
221 cp = cf_pair_find(cs, fmt);
222 if (!cp || !(value = cf_pair_value(cp))) {
228 strlcpy(out, value, outlen);
235 * Xlat for %{home_server:foo}
237 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
238 char *fmt, char *out, size_t outlen,
239 UNUSED RADIUS_ESCAPE_STRING func)
241 if (!fmt || !out || (outlen < 1)) return 0;
243 if (!request || !request->home_server) {
248 return xlat_cs(request->home_server->cs, fmt, out, outlen);
253 * Xlat for %{home_server_pool:foo}
255 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
256 char *fmt, char *out, size_t outlen,
257 UNUSED RADIUS_ESCAPE_STRING func)
259 if (!fmt || !out || (outlen < 1)) return 0;
261 if (!request || !request->home_pool) {
266 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
270 void realms_free(void)
274 rbtree_free(home_servers_bynumber);
275 home_servers_bynumber = NULL;
278 rbtree_free(home_servers_byname);
279 home_servers_byname = NULL;
281 rbtree_free(home_servers_byaddr);
282 home_servers_byaddr = NULL;
284 rbtree_free(home_pools_byname);
285 home_pools_byname = NULL;
288 rbtree_free(realms_byname);
289 realms_byname = NULL;
293 realm_regex_t *this, *next;
295 for (this = realms_regex; this != NULL; this = next) {
310 static CONF_PARSER limit_config[] = {
311 { "max_connections", PW_TYPE_INTEGER,
312 offsetof(home_server, max_connections), NULL, "16" },
314 { "max_requests", PW_TYPE_INTEGER,
315 offsetof(home_server,max_requests), NULL, "0" },
317 { "lifetime", PW_TYPE_INTEGER,
318 offsetof(home_server,lifetime), NULL, "0" },
320 { "idle_timeout", PW_TYPE_INTEGER,
321 offsetof(home_server,idle_timeout), NULL, "0" },
323 { NULL, -1, 0, NULL, NULL } /* end the list */
326 static struct in_addr hs_ip4addr;
327 static struct in6_addr hs_ip6addr;
328 static char *hs_srcipaddr = NULL;
329 static char *hs_type = NULL;
330 static char *hs_check = NULL;
331 static char *hs_virtual_server = NULL;
333 static char *hs_proto = NULL;
337 static CONF_PARSER home_server_coa[] = {
338 { "irt", PW_TYPE_INTEGER,
339 offsetof(home_server, coa_irt), 0, Stringify(2) },
340 { "mrt", PW_TYPE_INTEGER,
341 offsetof(home_server, coa_mrt), 0, Stringify(16) },
342 { "mrc", PW_TYPE_INTEGER,
343 offsetof(home_server, coa_mrc), 0, Stringify(5) },
344 { "mrd", PW_TYPE_INTEGER,
345 offsetof(home_server, coa_mrd), 0, Stringify(30) },
347 { NULL, -1, 0, NULL, NULL } /* end the list */
351 static CONF_PARSER home_server_config[] = {
352 { "ipaddr", PW_TYPE_IPADDR,
353 0, &hs_ip4addr, NULL },
354 { "ipv6addr", PW_TYPE_IPV6ADDR,
355 0, &hs_ip6addr, NULL },
356 { "virtual_server", PW_TYPE_STRING_PTR,
357 0, &hs_virtual_server, NULL },
359 { "port", PW_TYPE_INTEGER,
360 offsetof(home_server,port), NULL, "0" },
362 { "type", PW_TYPE_STRING_PTR,
366 { "proto", PW_TYPE_STRING_PTR,
367 0, &hs_proto, NULL },
370 { "secret", PW_TYPE_STRING_PTR,
371 offsetof(home_server,secret), NULL, NULL},
373 { "src_ipaddr", PW_TYPE_STRING_PTR,
374 0, &hs_srcipaddr, NULL },
376 { "response_window", PW_TYPE_INTEGER,
377 offsetof(home_server,response_window), NULL, "30" },
378 { "no_response_fail", PW_TYPE_BOOLEAN,
379 offsetof(home_server,no_response_fail), NULL, NULL },
380 { "max_outstanding", PW_TYPE_INTEGER,
381 offsetof(home_server,max_outstanding), NULL, "65536" },
382 { "require_message_authenticator", PW_TYPE_BOOLEAN,
383 offsetof(home_server, message_authenticator), 0, NULL },
385 { "zombie_period", PW_TYPE_INTEGER,
386 offsetof(home_server,zombie_period), NULL, "40" },
387 { "status_check", PW_TYPE_STRING_PTR,
388 0, &hs_check, "none" },
389 { "ping_check", PW_TYPE_STRING_PTR,
390 0, &hs_check, NULL },
392 { "ping_interval", PW_TYPE_INTEGER,
393 offsetof(home_server,ping_interval), NULL, "30" },
394 { "check_interval", PW_TYPE_INTEGER,
395 offsetof(home_server,ping_interval), NULL, "30" },
396 { "num_answers_to_alive", PW_TYPE_INTEGER,
397 offsetof(home_server,num_pings_to_alive), NULL, "3" },
398 { "num_pings_to_alive", PW_TYPE_INTEGER,
399 offsetof(home_server,num_pings_to_alive), NULL, "3" },
400 { "revive_interval", PW_TYPE_INTEGER,
401 offsetof(home_server,revive_interval), NULL, "300" },
402 { "status_check_timeout", PW_TYPE_INTEGER,
403 offsetof(home_server,ping_timeout), NULL, "4" },
405 { "username", PW_TYPE_STRING_PTR,
406 offsetof(home_server,ping_user_name), NULL, NULL},
407 { "password", PW_TYPE_STRING_PTR,
408 offsetof(home_server,ping_user_password), NULL, NULL},
411 { "historic_average_window", PW_TYPE_INTEGER,
412 offsetof(home_server,ema.window), NULL, NULL },
416 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (const void *) home_server_coa },
419 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
421 { NULL, -1, 0, NULL, NULL } /* end the list */
425 static void null_free(UNUSED void *data)
429 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
437 free(hs_virtual_server); /* used only for printing during parsing */
438 hs_virtual_server = NULL;
440 name2 = cf_section_name2(cs);
442 cf_log_err(cf_sectiontoitem(cs),
443 "Home server section is missing a name.");
447 home = rad_malloc(sizeof(*home));
448 memset(home, 0, sizeof(*home));
454 * For zombie period calculations. We want to count
455 * zombies from the time when the server starts, instead
458 home->last_packet = time(NULL);
461 * Authentication servers have a default "no_response_fail = 0".
462 * Accounting servers have a default "no_response_fail = 1".
464 * This is because authentication packets are retried, so
465 * they can fail over to another home server. Accounting
466 * packets are not retried, so they cannot fail over, and
467 * instead should be rejected immediately.
469 home->no_response_fail = 2;
471 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
472 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
473 if (cf_section_parse(cs, home, home_server_config) < 0) {
478 * Figure out which one to use.
480 if (cf_pair_find(cs, "ipaddr")) {
481 home->ipaddr.af = AF_INET;
482 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
484 } else if (cf_pair_find(cs, "ipv6addr")) {
485 home->ipaddr.af = AF_INET6;
486 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
488 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
489 home->ipaddr.af = AF_UNSPEC;
490 home->server = cf_pair_value(cp);
492 cf_log_err(cf_sectiontoitem(cs),
493 "Invalid value for virtual_server");
497 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
499 cf_log_err(cf_sectiontoitem(cs),
500 "No such server %s", home->server);
505 * When CoA is used, the user has to specify the type
506 * of the home server, even when they point to
509 home->secret = strdup("");
513 cf_log_err(cf_sectiontoitem(cs),
514 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
531 if (!home->port || (home->port > 65535)) {
532 cf_log_err(cf_sectiontoitem(cs),
533 "No port, or invalid port defined for home server %s.",
539 cf_log_err(cf_sectiontoitem(cs),
540 "Fatal error! Home server %s is ourselves!",
546 cf_log_err(cf_sectiontoitem(cs),
547 "No shared secret defined for home server %s.",
553 * Use a reasonable default.
556 if (!hs_type) hs_type = strdup("auth+acct");
558 if (strcasecmp(hs_type, "auth") == 0) {
559 home->type = HOME_TYPE_AUTH;
560 if (home->no_response_fail == 2) home->no_response_fail = 0;
562 } else if (strcasecmp(hs_type, "acct") == 0) {
563 home->type = HOME_TYPE_ACCT;
564 if (home->no_response_fail == 2) home->no_response_fail = 1;
566 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
567 home->type = HOME_TYPE_AUTH;
571 } else if (strcasecmp(hs_type, "coa") == 0) {
572 home->type = HOME_TYPE_COA;
575 if (home->server != NULL) {
576 cf_log_err(cf_sectiontoitem(cs),
577 "Home servers of type \"coa\" cannot point to a virtual server");
583 cf_log_err(cf_sectiontoitem(cs),
584 "Invalid type \"%s\" for home server %s.",
591 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
592 home->ping_check = HOME_PING_CHECK_NONE;
594 } else if (strcasecmp(hs_check, "status-server") == 0) {
595 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
597 } else if (strcasecmp(hs_check, "request") == 0) {
598 home->ping_check = HOME_PING_CHECK_REQUEST;
601 cf_log_err(cf_sectiontoitem(cs),
602 "Invalid ping_check \"%s\" for home server %s.",
609 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
610 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
611 if (!home->ping_user_name) {
612 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
616 if ((home->type == HOME_TYPE_AUTH) &&
617 !home->ping_user_password) {
618 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
623 home->proto = IPPROTO_UDP;
626 if (strcmp(hs_proto, "udp") == 0) {
630 } else if (strcmp(hs_proto, "tcp") == 0) {
633 home->proto = IPPROTO_TCP;
635 if (home->ping_check != HOME_PING_CHECK_NONE) {
636 cf_log_err(cf_sectiontoitem(cs),
637 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
642 cf_log_err(cf_sectiontoitem(cs),
643 "Unknown proto \"%s\".", hs_proto);
650 rbtree_finddata(home_servers_byaddr, home)) {
651 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
656 * Check the TLS configuration.
658 tls = cf_section_sub_find(cs, "tls");
661 * If the home is a virtual server, don't look up source IP.
664 rad_assert(home->ipaddr.af != AF_UNSPEC);
667 * Otherwise look up the source IP using the same
668 * address family as the destination IP.
671 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
672 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
678 * Source isn't specified: Source is
679 * the correct address family, but all zeros.
681 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
682 home->src_ipaddr.af = home->ipaddr.af;
685 if (tls && (home->proto != IPPROTO_TCP)) {
686 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available for UDP sockets.");
693 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available in this executable.");
698 * Parse the SSL client configuration.
701 home->tls = tls_client_conf_parse(tls);
709 cf_log_err(cf_sectiontoitem(cs), "Virtual home_servers cannot have a \"tls\" subsection");
714 * Make sure that this is set.
716 if (home->src_ipaddr.af == AF_UNSPEC) {
717 home->src_ipaddr.af = home->ipaddr.af;
723 if (rbtree_finddata(home_servers_byname, home) != NULL) {
724 cf_log_err(cf_sectiontoitem(cs),
725 "Duplicate home server name %s.", name2);
730 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
731 cf_log_err(cf_sectiontoitem(cs),
732 "Duplicate home server IP %s.", name2);
736 if (!rbtree_insert(home_servers_byname, home)) {
737 cf_log_err(cf_sectiontoitem(cs),
738 "Internal error %d adding home server %s.",
744 !rbtree_insert(home_servers_byaddr, home)) {
745 rbtree_deletebydata(home_servers_byname, home);
746 cf_log_err(cf_sectiontoitem(cs),
747 "Internal error %d adding home server %s.",
753 home->number = home_server_max_number++;
754 if (!rbtree_insert(home_servers_bynumber, home)) {
755 rbtree_deletebydata(home_servers_byname, home);
756 if (home->ipaddr.af != AF_UNSPEC) {
757 rbtree_deletebydata(home_servers_byname, home);
759 cf_log_err(cf_sectiontoitem(cs),
760 "Internal error %d adding home server %s.",
766 if (home->max_outstanding < 8) home->max_outstanding = 8;
767 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
769 if (home->ping_interval < 6) home->ping_interval = 6;
770 if (home->ping_interval > 120) home->ping_interval = 120;
772 if (home->response_window < 1) home->response_window = 1;
773 if (home->response_window > 60) home->response_window = 60;
774 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
776 if (home->zombie_period < 1) home->zombie_period = 1;
777 if (home->zombie_period > 120) home->zombie_period = 120;
779 if (home->zombie_period < home->response_window) {
780 home->zombie_period = home->response_window;
783 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
784 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
786 if (home->ping_timeout < 3) home->ping_timeout = 3;
787 if (home->ping_timeout > 10) home->ping_timeout = 10;
789 if (home->revive_interval < 60) home->revive_interval = 60;
790 if (home->revive_interval > 3600) home->revive_interval = 3600;
793 if (home->coa_irt < 1) home->coa_irt = 1;
794 if (home->coa_irt > 5) home->coa_irt = 5;
796 if (home->coa_mrc < 0) home->coa_mrc = 0;
797 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
799 if (home->coa_mrt < 0) home->coa_mrt = 0;
800 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
802 if (home->coa_mrd < 5) home->coa_mrd = 5;
803 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
806 if (home->max_connections > 1024) home->max_connections = 1024;
810 * UDP sockets can't be connection limited.
812 if (home->proto != IPPROTO_TCP) home->max_connections = 0;
815 if ((home->idle_timeout > 0) && (home->idle_timeout < 5))
816 home->idle_timeout = 5;
817 if ((home->lifetime > 0) && (home->lifetime < 5))
819 if ((home->lifetime > 0) && (home->idle_timeout > home->lifetime))
820 home->idle_timeout = 0;
822 tls = cf_item_parent(cf_sectiontoitem(cs));
823 if (strcmp(cf_section_name1(tls), "server") == 0) {
824 home->parent_server = cf_section_name2(tls);
828 home_server *home2 = rad_malloc(sizeof(*home2));
830 memcpy(home2, home, sizeof(*home2));
832 home2->type = HOME_TYPE_ACCT;
834 home2->ping_user_password = NULL;
836 home2->parent_server = home->parent_server;
838 if (home->no_response_fail == 2) home->no_response_fail = 0;
839 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
841 if (!rbtree_insert(home_servers_byname, home2)) {
842 cf_log_err(cf_sectiontoitem(cs),
843 "Internal error %d adding home server %s.",
850 !rbtree_insert(home_servers_byaddr, home2)) {
851 rbtree_deletebydata(home_servers_byname, home2);
852 cf_log_err(cf_sectiontoitem(cs),
853 "Internal error %d adding home server %s.",
860 home2->number = home_server_max_number++;
861 if (!rbtree_insert(home_servers_bynumber, home2)) {
862 rbtree_deletebydata(home_servers_byname, home2);
863 if (!home2->server) {
864 rbtree_deletebydata(home_servers_byname, home2);
866 cf_log_err(cf_sectiontoitem(cs),
867 "Internal error %d adding home server %s.",
876 * Mark it as already processed
878 cf_data_add(cs, "home_server", null_free, null_free);
884 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
885 int server_type, int num_home_servers)
889 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
891 if (!pool) return NULL; /* just for pairanoia */
893 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
898 pool->server_type = server_type;
899 pool->num_home_servers = num_home_servers;
904 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
905 const char *name, int server_type,
908 home_server myhome, *home;
909 CONF_SECTION *server_cs;
912 cf_log_err(cf_pairtoitem(cp),
913 "No value given for home_server.");
918 myhome.type = server_type;
919 home = rbtree_finddata(home_servers_byname, &myhome);
925 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
927 cf_log_err(cf_pairtoitem(cp),
928 "Unknown home_server \"%s\".", name);
932 home = rbtree_finddata(home_servers_byname, &myhome);
934 cf_log_err(cf_pairtoitem(cp),
935 "Internal error %d adding home server \"%s\".",
945 static int server_pool_add(realm_config_t *rc,
946 CONF_SECTION *cs, int server_type, int do_print)
949 home_pool_t *pool = NULL;
952 int num_home_servers;
955 name2 = cf_section_name1(cs);
956 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
957 (strcasecmp(name2, "home_server_pool") != 0))) {
958 cf_log_err(cf_sectiontoitem(cs),
959 "Section is not a home_server_pool.");
963 name2 = cf_section_name2(cs);
965 cf_log_err(cf_sectiontoitem(cs),
966 "Server pool section is missing a name.");
971 * Count the home servers and initalize them.
973 num_home_servers = 0;
974 for (cp = cf_pair_find(cs, "home_server");
976 cp = cf_pair_find_next(cs, cp, "home_server")) {
979 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
980 server_type, &home)) {
985 if (num_home_servers == 0) {
986 cf_log_err(cf_sectiontoitem(cs),
987 "No home servers defined in pool %s",
992 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
998 * Fallback servers must be defined, and must be
1001 cp = cf_pair_find(cs, "fallback");
1004 if (server_type == HOME_TYPE_COA) {
1005 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
1010 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
1011 server_type, &pool->fallback)) {
1016 if (!pool->fallback->server) {
1017 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
1022 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
1024 cp = cf_pair_find(cs, "type");
1026 static FR_NAME_NUMBER pool_types[] = {
1027 { "load-balance", HOME_POOL_LOAD_BALANCE },
1028 { "fail-over", HOME_POOL_FAIL_OVER },
1029 { "round_robin", HOME_POOL_LOAD_BALANCE },
1030 { "fail_over", HOME_POOL_FAIL_OVER },
1031 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1032 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1033 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1037 value = cf_pair_value(cp);
1039 cf_log_err(cf_pairtoitem(cp),
1040 "No value given for type.");
1044 pool->type = fr_str2int(pool_types, value, 0);
1046 cf_log_err(cf_pairtoitem(cp),
1047 "Unknown type \"%s\".",
1052 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1055 cp = cf_pair_find(cs, "virtual_server");
1057 pool->virtual_server = cf_pair_value(cp);
1058 if (!pool->virtual_server) {
1059 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
1064 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1067 if (!cf_section_sub_find_name2(rc->cs, "server",
1068 pool->virtual_server)) {
1069 cf_log_err(cf_pairtoitem(cp), "No such server %s",
1070 pool->virtual_server);
1076 num_home_servers = 0;
1077 for (cp = cf_pair_find(cs, "home_server");
1079 cp = cf_pair_find_next(cs, cp, "home_server")) {
1082 value = cf_pair_value(cp);
1084 memset(&myhome, 0, sizeof(myhome));
1085 myhome.name = value;
1086 myhome.type = server_type;
1088 home = rbtree_finddata(home_servers_byname, &myhome);
1090 DEBUG2("Internal sanity check failed");
1095 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1099 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1100 pool->servers[num_home_servers++] = home;
1101 } /* loop over home_server's */
1103 if (pool->fallback && do_print) {
1104 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1107 if (!rbtree_insert(home_pools_byname, pool)) {
1108 rad_assert("Internal sanity check failed");
1112 if (do_print) cf_log_info(cs, " }");
1114 cf_data_add(cs, "home_server_pool", pool, free);
1116 rad_assert(pool->server_type != 0);
1121 if (do_print) cf_log_info(cs, " }");
1127 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1129 const char *name, const char *secret,
1130 home_pool_type_t ldflag, home_pool_t **pool_p,
1131 int type, const char *server)
1134 int i, insert_point, num_home_servers;
1135 home_server myhome, *home;
1136 home_pool_t mypool, *pool;
1137 CONF_SECTION *subcs;
1139 rc = rc; /* -Wunused */
1148 * LOCAL realms get sanity checked, and nothing else happens.
1150 if (strcmp(name, "LOCAL") == 0) {
1152 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1159 return 0; /* Not proxying. Can't do non-LOCAL realms */
1162 mypool.name = realm;
1163 mypool.server_type = type;
1164 pool = rbtree_finddata(home_pools_byname, &mypool);
1166 if (pool->type != ldflag) {
1167 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1171 if (pool->server_type != type) {
1172 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1179 home = rbtree_finddata(home_servers_byname, &myhome);
1181 if (secret && (strcmp(home->secret, secret) != 0)) {
1182 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1186 if (home->type != type) {
1187 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1192 * See if the home server is already listed
1193 * in the pool. If so, do nothing else.
1195 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1196 if (pool->servers[i] == home) {
1203 * If we do have a pool, check that there is room to
1204 * insert the home server we've found, or the one that we
1207 * Note that we insert it into the LAST available
1208 * position, in order to maintain the same order as in
1209 * the configuration files.
1213 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1214 if (pool->servers[i]) break;
1216 if (!pool->servers[i]) {
1221 if (insert_point < 0) {
1222 cf_log_err(cf_sectiontoitem(cs), "No room in pool to add home server \"%s\". Please update the realm configuration to use the new-style home servers and server pools.", name);
1228 * No home server, allocate one.
1234 home = rad_malloc(sizeof(*home));
1235 memset(home, 0, sizeof(*home));
1238 home->hostname = name;
1240 home->secret = secret;
1242 home->proto = IPPROTO_UDP;
1244 p = strchr(name, ':');
1246 if (type == HOME_TYPE_AUTH) {
1247 home->port = PW_AUTH_UDP_PORT;
1249 home->port = PW_ACCT_UDP_PORT;
1255 } else if (p == name) {
1256 cf_log_err(cf_sectiontoitem(cs),
1257 "Invalid hostname %s.",
1263 home->port = atoi(p + 1);
1264 if ((home->port == 0) || (home->port > 65535)) {
1265 cf_log_err(cf_sectiontoitem(cs),
1272 q = rad_malloc((p - name) + 1);
1273 memcpy(q, name, (p - name));
1279 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1280 cf_log_err(cf_sectiontoitem(cs),
1281 "Failed looking up hostname %s.",
1287 home->src_ipaddr.af = home->ipaddr.af;
1289 home->ipaddr.af = AF_UNSPEC;
1290 home->server = server;
1295 * Use the old-style configuration.
1297 home->max_outstanding = 65535*16;
1298 home->zombie_period = rc->retry_delay * rc->retry_count;
1299 if (home->zombie_period == 0) home->zombie_period =30;
1300 home->response_window = home->zombie_period - 1;
1302 home->ping_check = HOME_PING_CHECK_NONE;
1304 home->revive_interval = rc->dead_time;
1306 if (rbtree_finddata(home_servers_byaddr, home)) {
1307 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1312 if (!rbtree_insert(home_servers_byname, home)) {
1313 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1318 if (!rbtree_insert(home_servers_byaddr, home)) {
1319 rbtree_deletebydata(home_servers_byname, home);
1320 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1326 home->number = home_server_max_number++;
1327 if (!rbtree_insert(home_servers_bynumber, home)) {
1328 rbtree_deletebydata(home_servers_byname, home);
1329 if (home->ipaddr.af != AF_UNSPEC) {
1330 rbtree_deletebydata(home_servers_byname, home);
1332 cf_log_err(cf_sectiontoitem(cs),
1333 "Internal error %d adding home server %s.",
1342 * We now have a home server, see if we can insert it
1343 * into pre-existing pool.
1345 if (insert_point >= 0) {
1346 rad_assert(pool != NULL);
1347 pool->servers[insert_point] = home;
1351 rad_assert(pool == NULL);
1352 rad_assert(home != NULL);
1355 * Count the old-style realms of this name.
1357 num_home_servers = 0;
1358 for (subcs = cf_section_find_next(cs, NULL, "realm");
1360 subcs = cf_section_find_next(cs, subcs, "realm")) {
1361 const char *this = cf_section_name2(subcs);
1363 if (!this || (strcmp(this, realm) != 0)) continue;
1367 if (num_home_servers == 0) {
1368 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1373 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1376 pool->servers[0] = home;
1378 if (!rbtree_insert(home_pools_byname, pool)) {
1379 rad_assert("Internal sanity check failed");
1389 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1392 const char *secret = NULL;
1393 home_pool_type_t ldflag;
1396 cp = cf_pair_find(cs, "ldflag");
1397 ldflag = HOME_POOL_FAIL_OVER;
1399 host = cf_pair_value(cp);
1401 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1405 if (strcasecmp(host, "fail_over") == 0) {
1406 cf_log_info(cs, "\tldflag = fail_over");
1408 } else if (strcasecmp(host, "round_robin") == 0) {
1409 ldflag = HOME_POOL_LOAD_BALANCE;
1410 cf_log_info(cs, "\tldflag = round_robin");
1413 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1416 } /* else don't print it. */
1419 * Allow old-style if it doesn't exist, or if it exists and
1422 cp = cf_pair_find(cs, "authhost");
1424 host = cf_pair_value(cp);
1426 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1430 if (strcmp(host, "LOCAL") != 0) {
1431 cp = cf_pair_find(cs, "secret");
1433 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1437 secret = cf_pair_value(cp);
1439 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1444 cf_log_info(cs, "\tauthhost = %s", host);
1446 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1447 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1452 cp = cf_pair_find(cs, "accthost");
1454 host = cf_pair_value(cp);
1456 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1461 * Don't look for a secret again if it was found
1464 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1465 cp = cf_pair_find(cs, "secret");
1467 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1471 secret = cf_pair_value(cp);
1473 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1478 cf_log_info(cs, "\taccthost = %s", host);
1480 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1481 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1486 cp = cf_pair_find(cs, "virtual_server");
1488 host = cf_pair_value(cp);
1490 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1494 cf_log_info(cs, "\tvirtual_server = %s", host);
1496 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1497 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1500 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1501 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1506 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1514 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1515 const char *name, home_pool_t **dest,
1516 int server_type, int do_print)
1518 home_pool_t mypool, *pool;
1521 mypool.server_type = server_type;
1523 pool = rbtree_finddata(home_pools_byname, &mypool);
1525 CONF_SECTION *pool_cs;
1527 pool_cs = cf_section_sub_find_name2(rc->cs,
1531 pool_cs = cf_section_sub_find_name2(rc->cs,
1536 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1540 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1544 pool = rbtree_finddata(home_pools_byname, &mypool);
1546 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1551 if (pool->server_type != server_type) {
1552 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1563 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1569 home_pool_t *auth_pool, *acct_pool;
1570 const char *auth_pool_name, *acct_pool_name;
1573 name2 = cf_section_name1(cs);
1574 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1575 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1579 name2 = cf_section_name2(cs);
1581 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1586 auth_pool = acct_pool = NULL;
1587 auth_pool_name = acct_pool_name = NULL;
1590 * Prefer new configuration to old one.
1592 cp = cf_pair_find(cs, "pool");
1593 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1594 if (cp) auth_pool_name = cf_pair_value(cp);
1595 if (cp && auth_pool_name) {
1596 acct_pool_name = auth_pool_name;
1597 if (!add_pool_to_realm(rc, cs,
1598 auth_pool_name, &auth_pool,
1599 HOME_TYPE_AUTH, 1)) {
1602 if (!add_pool_to_realm(rc, cs,
1603 auth_pool_name, &acct_pool,
1604 HOME_TYPE_ACCT, 0)) {
1609 cp = cf_pair_find(cs, "auth_pool");
1610 if (cp) auth_pool_name = cf_pair_value(cp);
1611 if (cp && auth_pool_name) {
1613 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1616 if (!add_pool_to_realm(rc, cs,
1617 auth_pool_name, &auth_pool,
1618 HOME_TYPE_AUTH, 1)) {
1623 cp = cf_pair_find(cs, "acct_pool");
1624 if (cp) acct_pool_name = cf_pair_value(cp);
1625 if (cp && acct_pool_name) {
1626 int do_print = TRUE;
1629 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1633 if (!auth_pool || auth_pool_name &&
1634 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1638 if (!add_pool_to_realm(rc, cs,
1639 acct_pool_name, &acct_pool,
1640 HOME_TYPE_ACCT, do_print)) {
1646 cf_log_info(cs, " realm %s {", name2);
1650 * The realm MAY already exist if it's an old-style realm.
1651 * In that case, merge the old-style realm with this one.
1653 r = realm_find2(name2);
1654 if (r && (strcmp(r->name, name2) == 0)) {
1655 if (cf_pair_find(cs, "auth_pool") ||
1656 cf_pair_find(cs, "acct_pool")) {
1657 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1661 if (!old_realm_config(rc, cs, r)) {
1665 cf_log_info(cs, " } # realm %s", name2);
1671 if (name2[0] == '~') {
1676 * Include substring matches.
1678 rcode = regcomp(®, name2 + 1,
1679 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1683 regerror(rcode, ®, buffer, sizeof(buffer));
1685 cf_log_err(cf_sectiontoitem(cs),
1686 "Invalid regex \"%s\": %s",
1694 r = rad_malloc(sizeof(*r));
1695 memset(r, 0, sizeof(*r));
1700 r->auth_pool = auth_pool;
1701 r->acct_pool = acct_pool;
1703 if (auth_pool_name &&
1704 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1705 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1707 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1708 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1712 cp = cf_pair_find(cs, "nostrip");
1713 if (cp && (cf_pair_value(cp) == NULL)) {
1715 cf_log_info(cs, "\tnostrip");
1719 * We're a new-style realm. Complain if we see the old
1722 if (r->auth_pool || r->acct_pool) {
1723 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1724 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1725 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1726 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1727 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1732 * The realm MAY be an old-style realm, as there
1733 * was no auth_pool or acct_pool. Double-check
1734 * it, just to be safe.
1736 } else if (!old_realm_config(rc, cs, r)) {
1742 * It's a regex. Add it to a separate list.
1744 if (name2[0] == '~') {
1745 realm_regex_t *rr, **last;
1747 rr = rad_malloc(sizeof(*rr));
1749 last = &realms_regex;
1750 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1758 cf_log_info(cs, " }");
1763 if (!rbtree_insert(realms_byname, r)) {
1764 rad_assert("Internal sanity check failed");
1768 cf_log_info(cs, " }");
1773 cf_log_info(cs, " } # realm %s", name2);
1779 static const FR_NAME_NUMBER home_server_types[] = {
1780 { "auth", HOME_TYPE_AUTH },
1781 { "auth+acct", HOME_TYPE_AUTH },
1782 { "acct", HOME_TYPE_ACCT },
1783 { "coa", HOME_TYPE_COA },
1787 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1790 const char *name, *type;
1792 CONF_SECTION *server_cs;
1794 cp = cf_pair_find(cs, "home_server");
1796 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1797 return HOME_TYPE_INVALID;
1800 name = cf_pair_value(cp);
1802 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1803 return HOME_TYPE_INVALID;
1806 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1808 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1809 return HOME_TYPE_INVALID;
1812 cp = cf_pair_find(server_cs, "type");
1814 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1815 return HOME_TYPE_INVALID;
1818 type = cf_pair_value(cp);
1820 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1821 return HOME_TYPE_INVALID;
1824 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1825 if (home == HOME_TYPE_INVALID) {
1826 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1827 return HOME_TYPE_INVALID;
1830 return home; /* 'cause we miss it so much */
1834 int realms_init(CONF_SECTION *config)
1838 CONF_SECTION *server_cs;
1840 realm_config_t *rc, *old_rc;
1842 if (realms_byname) return 1;
1844 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1845 if (!realms_byname) {
1851 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1852 if (!home_servers_byaddr) {
1857 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1858 if (!home_servers_byname) {
1864 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1865 if (!home_servers_bynumber) {
1871 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1872 if (!home_pools_byname) {
1878 rc = rad_malloc(sizeof(*rc));
1879 memset(rc, 0, sizeof(*rc));
1883 cs = cf_subsection_find_next(config, NULL, "proxy");
1885 cf_section_parse(cs, rc, proxy_config);
1887 rc->dead_time = DEAD_TIME;
1888 rc->retry_count = RETRY_COUNT;
1889 rc->retry_delay = RETRY_DELAY;
1891 rc->wake_all_if_all_dead= 0;
1894 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1896 cs = cf_subsection_find_next(config, cs, "home_server")) {
1897 if (!home_server_add(rc, cs)) {
1905 * Loop over virtual servers to find homes which are
1908 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1910 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1911 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1913 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1914 if (!home_server_add(rc, cs)) {
1923 for (cs = cf_subsection_find_next(config, NULL, "realm");
1925 cs = cf_subsection_find_next(config, cs, "realm")) {
1926 if (!realm_add(rc, cs)) {
1935 * CoA pools aren't tied to realms.
1937 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1939 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1943 * Pool was already loaded.
1945 if (cf_data_find(cs, "home_server_pool")) continue;
1947 type = pool_peek_type(config, cs);
1948 if (type == HOME_TYPE_INVALID) {
1954 if (!server_pool_add(rc, cs, type, TRUE)) {
1964 xlat_register("home_server", xlat_home_server, NULL);
1965 xlat_register("home_server_pool", xlat_server_pool, NULL);
1969 * Swap pointers atomically.
1971 old_rc = realm_config;
1979 * Find a realm where "name" might be the regex.
1981 REALM *realm_find2(const char *name)
1986 if (!name) name = "NULL";
1988 myrealm.name = name;
1989 realm = rbtree_finddata(realms_byname, &myrealm);
1990 if (realm) return realm;
1994 realm_regex_t *this;
1996 for (this = realms_regex; this != NULL; this = this->next) {
1997 if (strcmp(this->realm->name, name) == 0) {
2005 * Couldn't find a realm. Look for DEFAULT.
2007 myrealm.name = "DEFAULT";
2008 return rbtree_finddata(realms_byname, &myrealm);
2013 * Find a realm in the REALM list.
2015 REALM *realm_find(const char *name)
2020 if (!name) name = "NULL";
2022 myrealm.name = name;
2023 realm = rbtree_finddata(realms_byname, &myrealm);
2024 if (realm) return realm;
2028 realm_regex_t *this;
2030 for (this = realms_regex; this != NULL; this = this->next) {
2035 * Include substring matches.
2037 if (regcomp(®, this->realm->name + 1,
2038 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2042 compare = regexec(®, name, 0, NULL, 0);
2045 if (compare == 0) return this->realm;
2051 * Couldn't find a realm. Look for DEFAULT.
2053 myrealm.name = "DEFAULT";
2054 return rbtree_finddata(realms_byname, &myrealm);
2059 home_server *home_server_ldb(const char *realmname,
2060 home_pool_t *pool, REQUEST *request)
2064 home_server *found = NULL;
2065 home_server *zombie = NULL;
2069 * Determine how to pick choose the home server.
2071 switch (pool->type) {
2075 * For load-balancing by client IP address, we
2076 * pick a home server by hashing the client IP.
2078 * This isn't as even a load distribution as
2079 * tracking the State attribute, but it's better
2082 case HOME_POOL_CLIENT_BALANCE:
2083 switch (request->packet->src_ipaddr.af) {
2085 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2086 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2089 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2090 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2096 start = hash % pool->num_home_servers;
2099 case HOME_POOL_CLIENT_PORT_BALANCE:
2100 switch (request->packet->src_ipaddr.af) {
2102 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2103 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2106 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2107 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2113 fr_hash_update(&request->packet->src_port,
2114 sizeof(request->packet->src_port), hash);
2115 start = hash % pool->num_home_servers;
2118 case HOME_POOL_KEYED_BALANCE:
2119 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0)) != NULL) {
2120 hash = fr_hash(vp->vp_strvalue, vp->length);
2121 start = hash % pool->num_home_servers;
2126 case HOME_POOL_LOAD_BALANCE:
2127 case HOME_POOL_FAIL_OVER:
2131 default: /* this shouldn't happen... */
2138 * Starting with the home server we chose, loop through
2139 * all home servers. If the current one is dead, skip
2140 * it. If it is too busy, skip it.
2142 * Otherwise, use it.
2144 for (count = 0; count < pool->num_home_servers; count++) {
2145 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2147 if (!home) continue;
2150 * Skip dead home servers.
2152 if (home->state == HOME_STATE_IS_DEAD) {
2157 * This home server is too busy. Choose another one.
2159 if (home->currently_outstanding >= home->max_outstanding) {
2165 * We read the packet from a detail file, AND it
2166 * came from this server. Don't re-proxy it
2169 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2170 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2171 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2177 * Default virtual: ignore homes tied to a
2180 if (!request->server && home->parent_server) {
2185 * A virtual AND home is tied to virtual,
2186 * ignore ones which don't match.
2188 if (request->server && home->parent_server &&
2189 strcmp(request->server, home->parent_server) != 0) {
2194 * Allow request->server && !home->parent_server
2196 * i.e. virtuals can proxy to globally defined
2201 * It's zombie, so we remember the first zombie
2202 * we find, but we don't mark it as a "live"
2205 if (home->state == HOME_STATE_ZOMBIE) {
2206 if (!zombie) zombie = home;
2211 * We've found the first "live" one. Use that.
2213 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2219 * Otherwise we're doing some kind of load balancing.
2220 * If we haven't found one yet, pick this one.
2227 RDEBUG3("PROXY %s %d\t%s %d",
2228 found->name, found->currently_outstanding,
2229 home->name, home->currently_outstanding);
2232 * Prefer this server if it's less busy than the
2233 * one we had previously found.
2235 if (home->currently_outstanding < found->currently_outstanding) {
2236 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2237 home->name, found->name);
2243 * Ignore servers which are busier than the one
2246 if (home->currently_outstanding > found->currently_outstanding) {
2247 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2248 home->name, found->name);
2253 * From the list of servers which have the same
2254 * load, choose one at random.
2256 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2259 } /* loop over the home servers */
2262 * We have no live servers, BUT we have a zombie. Use
2263 * the zombie as a last resort.
2265 if (!found && zombie) {
2271 * There's a fallback if they're all dead.
2273 if (!found && pool->fallback) {
2274 found = pool->fallback;
2276 DEBUG("WARNING: Home server pool %s failing over to fallback %s",
2277 pool->name, found->server);
2278 if (pool->in_fallback) goto update_and_return;
2280 pool->in_fallback = TRUE;
2283 * Run the trigger once an hour saying that
2286 if ((pool->time_all_dead + 3600) < request->timestamp) {
2287 pool->time_all_dead = request->timestamp;
2288 exec_trigger(request, pool->cs, "home_server_pool.fallback");
2294 if ((found != pool->fallback) && pool->in_fallback) {
2295 pool->in_fallback = FALSE;
2296 exec_trigger(request, pool->cs, "home_server_pool.normal");
2300 * Allocate the proxy packet, only if it wasn't
2301 * already allocated by a module. This check is
2302 * mainly to support the proxying of EAP-TTLS and
2303 * EAP-PEAP tunneled requests.
2305 * In those cases, the EAP module creates a
2306 * "fake" request, and recursively passes it
2307 * through the authentication stage of the
2308 * server. The module then checks if the request
2309 * was supposed to be proxied, and if so, creates
2310 * a proxy packet from the TUNNELED request, and
2311 * not from the EAP request outside of the
2314 * The proxy then works like normal, except that
2315 * the response packet is "eaten" by the EAP
2316 * module, and encapsulated into an EAP packet.
2318 if (!request->proxy) {
2319 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2320 radlog(L_ERR|L_CONS, "no memory");
2325 * Copy the request, then look up name
2326 * and plain-text password in the copy.
2328 * Note that the User-Name attribute is
2329 * the *original* as sent over by the
2330 * client. The Stripped-User-Name
2331 * attribute is the one hacked through
2334 request->proxy->vps = paircopy(request->packet->vps);
2338 * Update the various fields as appropriate.
2340 request->proxy->src_ipaddr = found->src_ipaddr;
2341 request->proxy->src_port = 0;
2342 request->proxy->dst_ipaddr = found->ipaddr;
2343 request->proxy->dst_port = found->port;
2344 request->home_server = found;
2347 * We're supposed to add a Message-Authenticator
2348 * if it doesn't exist, and it doesn't exist.
2350 if (found->message_authenticator &&
2351 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2352 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0)) {
2353 radius_pairmake(request, &request->proxy->vps,
2354 "Message-Authenticator", "0x00",
2362 * No live match found, and no fallback to the "DEFAULT"
2363 * realm. We fix this by blindly marking all servers as
2364 * "live". But only do it for ones that don't support
2365 * "pings", as they will be marked live when they
2366 * actually are live.
2368 if (!realm_config->fallback &&
2369 realm_config->wake_all_if_all_dead) {
2370 for (count = 0; count < pool->num_home_servers; count++) {
2371 home_server *home = pool->servers[count];
2373 if (!home) continue;
2375 if ((home->state == HOME_STATE_IS_DEAD) &&
2376 (home->ping_check == HOME_PING_CHECK_NONE)) {
2377 home->state = HOME_STATE_ALIVE;
2378 if (!found) found = home;
2382 if (found) goto update_and_return;
2386 * Still nothing. Look up the DEFAULT realm, but only
2387 * if we weren't looking up the NULL or DEFAULT realms.
2389 if (realm_config->fallback &&
2391 (strcmp(realmname, "NULL") != 0) &&
2392 (strcmp(realmname, "DEFAULT") != 0)) {
2393 REALM *rd = realm_find("DEFAULT");
2395 if (!rd) return NULL;
2398 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2399 pool = rd->auth_pool;
2401 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2402 pool = rd->acct_pool;
2404 if (!pool) return NULL;
2406 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2407 return home_server_ldb(rd->name, pool, request);
2411 * Still haven't found anything. Oh well.
2417 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2421 memset(&myhome, 0, sizeof(myhome));
2422 myhome.ipaddr = *ipaddr;
2423 myhome.src_ipaddr.af = ipaddr->af;
2426 myhome.proto = proto;
2428 myhome.proto = IPPROTO_UDP;
2430 myhome.server = NULL; /* we're not called for internal proxying */
2432 return rbtree_finddata(home_servers_byaddr, &myhome);
2436 home_server *home_server_byname(const char *name, int type)
2440 memset(&myhome, 0, sizeof(myhome));
2444 return rbtree_finddata(home_servers_byname, &myhome);
2449 home_server *home_server_bynumber(int number)
2453 memset(&myhome, 0, sizeof(myhome));
2454 myhome.number = number;
2455 myhome.server = NULL; /* we're not called for internal proxying */
2457 return rbtree_finddata(home_servers_bynumber, &myhome);
2461 home_pool_t *home_pool_byname(const char *name, int type)
2465 memset(&mypool, 0, sizeof(mypool));
2467 mypool.server_type = type;
2468 return rbtree_finddata(home_pools_byname, &mypool);