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;
138 static int home_server_name_cmp(const void *one, const void *two)
140 const home_server *a = one;
141 const home_server *b = two;
143 if (a->type < b->type) return -1;
144 if (a->type > b->type) return +1;
146 return strcasecmp(a->name, b->name);
149 static int home_server_addr_cmp(const void *one, const void *two)
152 const home_server *a = one;
153 const home_server *b = two;
155 if (a->server && !b->server) return -1;
156 if (!a->server && b->server) return +1;
157 if (a->server && b->server) {
158 rcode = a->type - b->type;
159 if (rcode != 0) return rcode;
160 return strcmp(a->server, b->server);
163 if (a->port < b->port) return -1;
164 if (a->port > b->port) return +1;
167 if (a->proto < b->proto) return -1;
168 if (a->proto > b->proto) return +1;
171 rcode = fr_ipaddr_cmp(&a->src_ipaddr, &b->src_ipaddr);
172 if (rcode != 0) return rcode;
174 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
178 static int home_server_number_cmp(const void *one, const void *two)
180 const home_server *a = one;
181 const home_server *b = two;
183 return (a->number - b->number);
187 static int home_pool_name_cmp(const void *one, const void *two)
189 const home_pool_t *a = one;
190 const home_pool_t *b = two;
192 if (a->server_type < b->server_type) return -1;
193 if (a->server_type > b->server_type) return +1;
195 return strcasecmp(a->name, b->name);
199 static size_t xlat_cs(CONF_SECTION *cs, const char *fmt, char *out, size_t outlen)
202 const char *value = NULL;
207 if (strcmp(fmt, "instance") == 0) {
208 value = cf_section_name2(cs);
216 cp = cf_pair_find(cs, fmt);
217 if (!cp || !(value = cf_pair_value(cp))) {
223 strlcpy(out, value, outlen);
230 * Xlat for %{home_server:foo}
232 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
233 const char *fmt, char *out, size_t outlen)
235 if (!fmt || !out || (outlen < 1)) return 0;
237 if (!request || !request->home_server) {
242 return xlat_cs(request->home_server->cs, fmt, out, outlen);
247 * Xlat for %{home_server_pool:foo}
249 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
250 const char *fmt, char *out, size_t outlen)
252 if (!fmt || !out || (outlen < 1)) return 0;
254 if (!request || !request->home_pool) {
259 return xlat_cs(request->home_pool->cs, fmt, out, outlen);
263 void realms_free(void)
267 rbtree_free(home_servers_bynumber);
268 home_servers_bynumber = NULL;
271 rbtree_free(home_servers_byname);
272 home_servers_byname = NULL;
274 rbtree_free(home_servers_byaddr);
275 home_servers_byaddr = NULL;
277 rbtree_free(home_pools_byname);
278 home_pools_byname = NULL;
281 rbtree_free(realms_byname);
282 realms_byname = NULL;
286 realm_regex_t *this, *next;
288 for (this = realms_regex; this != NULL; this = next) {
303 static CONF_PARSER limit_config[] = {
304 { "max_connections", PW_TYPE_INTEGER,
305 offsetof(home_server, limit.max_connections), NULL, "16" },
307 { "max_requests", PW_TYPE_INTEGER,
308 offsetof(home_server, limit.max_requests), NULL, "0" },
310 { "lifetime", PW_TYPE_INTEGER,
311 offsetof(home_server, limit.lifetime), NULL, "0" },
313 { "idle_timeout", PW_TYPE_INTEGER,
314 offsetof(home_server, limit.idle_timeout), NULL, "0" },
316 { NULL, -1, 0, NULL, NULL } /* end the list */
319 static struct in_addr hs_ip4addr;
320 static struct in6_addr hs_ip6addr;
321 static char *hs_srcipaddr = NULL;
322 static char *hs_type = NULL;
323 static char *hs_check = NULL;
324 static char *hs_virtual_server = NULL;
326 static char *hs_proto = NULL;
330 static CONF_PARSER home_server_coa[] = {
331 { "irt", PW_TYPE_INTEGER,
332 offsetof(home_server, coa_irt), 0, Stringify(2) },
333 { "mrt", PW_TYPE_INTEGER,
334 offsetof(home_server, coa_mrt), 0, Stringify(16) },
335 { "mrc", PW_TYPE_INTEGER,
336 offsetof(home_server, coa_mrc), 0, Stringify(5) },
337 { "mrd", PW_TYPE_INTEGER,
338 offsetof(home_server, coa_mrd), 0, Stringify(30) },
340 { NULL, -1, 0, NULL, NULL } /* end the list */
344 static CONF_PARSER home_server_config[] = {
345 { "ipaddr", PW_TYPE_IPADDR,
346 0, &hs_ip4addr, NULL },
347 { "ipv6addr", PW_TYPE_IPV6ADDR,
348 0, &hs_ip6addr, NULL },
349 { "virtual_server", PW_TYPE_STRING_PTR,
350 0, &hs_virtual_server, NULL },
352 { "port", PW_TYPE_INTEGER,
353 offsetof(home_server,port), NULL, "0" },
355 { "type", PW_TYPE_STRING_PTR,
359 { "proto", PW_TYPE_STRING_PTR,
360 0, &hs_proto, NULL },
363 { "secret", PW_TYPE_STRING_PTR,
364 offsetof(home_server,secret), NULL, NULL},
366 { "src_ipaddr", PW_TYPE_STRING_PTR,
367 0, &hs_srcipaddr, NULL },
369 { "response_window", PW_TYPE_INTEGER,
370 offsetof(home_server,response_window), NULL, "30" },
371 { "max_outstanding", PW_TYPE_INTEGER,
372 offsetof(home_server,max_outstanding), NULL, "65536" },
374 { "zombie_period", PW_TYPE_INTEGER,
375 offsetof(home_server,zombie_period), NULL, "40" },
376 { "status_check", PW_TYPE_STRING_PTR,
377 0, &hs_check, "none" },
378 { "ping_check", PW_TYPE_STRING_PTR,
379 0, &hs_check, NULL },
381 { "ping_interval", PW_TYPE_INTEGER,
382 offsetof(home_server,ping_interval), NULL, "30" },
383 { "check_interval", PW_TYPE_INTEGER,
384 offsetof(home_server,ping_interval), NULL, "30" },
385 { "num_answers_to_alive", PW_TYPE_INTEGER,
386 offsetof(home_server,num_pings_to_alive), NULL, "3" },
387 { "revive_interval", PW_TYPE_INTEGER,
388 offsetof(home_server,revive_interval), NULL, "300" },
389 { "status_check_timeout", PW_TYPE_INTEGER,
390 offsetof(home_server,ping_timeout), NULL, "4" },
392 { "username", PW_TYPE_STRING_PTR,
393 offsetof(home_server,ping_user_name), NULL, NULL},
394 { "password", PW_TYPE_STRING_PTR,
395 offsetof(home_server,ping_user_password), NULL, NULL},
398 { "historic_average_window", PW_TYPE_INTEGER,
399 offsetof(home_server,ema.window), NULL, NULL },
403 { "coa", PW_TYPE_SUBSECTION, 0, NULL, (const void *) home_server_coa },
406 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
408 { NULL, -1, 0, NULL, NULL } /* end the list */
412 static void null_free(UNUSED void *data)
416 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
424 free(hs_virtual_server); /* used only for printing during parsing */
425 hs_virtual_server = NULL;
427 name2 = cf_section_name2(cs);
429 cf_log_err(cf_sectiontoitem(cs),
430 "Home server section is missing a name.");
434 home = rad_malloc(sizeof(*home));
435 memset(home, 0, sizeof(*home));
439 home->state = HOME_STATE_UNKNOWN;
442 * Last packet sent / received are zero.
445 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
446 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
447 if (cf_section_parse(cs, home, home_server_config) < 0) {
452 * Figure out which one to use.
454 if (cf_pair_find(cs, "ipaddr")) {
455 home->ipaddr.af = AF_INET;
456 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
458 } else if (cf_pair_find(cs, "ipv6addr")) {
459 home->ipaddr.af = AF_INET6;
460 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
462 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
463 home->ipaddr.af = AF_UNSPEC;
464 home->server = cf_pair_value(cp);
466 cf_log_err(cf_sectiontoitem(cs),
467 "Invalid value for virtual_server");
471 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
473 cf_log_err(cf_sectiontoitem(cs),
474 "No such server %s", home->server);
479 * When CoA is used, the user has to specify the type
480 * of the home server, even when they point to
483 home->secret = strdup("");
487 cf_log_err(cf_sectiontoitem(cs),
488 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
505 if (!home->port || (home->port > 65535)) {
506 cf_log_err(cf_sectiontoitem(cs),
507 "No port, or invalid port defined for home server %s.",
513 cf_log_err(cf_sectiontoitem(cs),
514 "Fatal error! Home server %s is ourselves!",
520 cf_log_err(cf_sectiontoitem(cs),
521 "No shared secret defined for home server %s.",
527 * Use a reasonable default.
530 if (!hs_type) hs_type = strdup("auth+acct");
532 if (strcasecmp(hs_type, "auth") == 0) {
533 home->type = HOME_TYPE_AUTH;
535 } else if (strcasecmp(hs_type, "acct") == 0) {
536 home->type = HOME_TYPE_ACCT;
538 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
539 home->type = HOME_TYPE_AUTH;
543 } else if (strcasecmp(hs_type, "coa") == 0) {
544 home->type = HOME_TYPE_COA;
547 if (home->server != NULL) {
548 cf_log_err(cf_sectiontoitem(cs),
549 "Home servers of type \"coa\" cannot point to a virtual server");
555 cf_log_err(cf_sectiontoitem(cs),
556 "Invalid type \"%s\" for home server %s.",
563 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
564 home->ping_check = HOME_PING_CHECK_NONE;
566 } else if (strcasecmp(hs_check, "status-server") == 0) {
567 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
569 } else if (strcasecmp(hs_check, "request") == 0) {
570 home->ping_check = HOME_PING_CHECK_REQUEST;
572 if (!home->ping_user_name ||
573 !*home->ping_user_name) {
574 cf_log_err(cf_sectiontoitem(cs), "You must supply a 'username' to enable status_check=request");
578 if ((home->type == HOME_TYPE_AUTH) &&
579 (!home->ping_user_password ||
580 !*home->ping_user_password)) {
581 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
586 cf_log_err(cf_sectiontoitem(cs),
587 "Invalid status__check \"%s\" for home server %s.",
594 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
595 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
596 if (!home->ping_user_name) {
597 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable status_check=request");
601 if ((home->type == HOME_TYPE_AUTH) &&
602 !home->ping_user_password) {
603 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable status_check=request");
608 home->proto = IPPROTO_UDP;
611 if (strcmp(hs_proto, "udp") == 0) {
615 } else if (strcmp(hs_proto, "tcp") == 0) {
618 home->proto = IPPROTO_TCP;
620 if (home->ping_check != HOME_PING_CHECK_NONE) {
621 cf_log_err(cf_sectiontoitem(cs),
622 "Only 'status_check = none' is allowed for home servers with 'proto = tcp'");
627 cf_log_err(cf_sectiontoitem(cs),
628 "Unknown proto \"%s\".", hs_proto);
635 rbtree_finddata(home_servers_byaddr, home)) {
636 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
641 * Check the TLS configuration.
643 tls = cf_section_sub_find(cs, "tls");
646 * If the home is a virtual server, don't look up source IP.
649 rad_assert(home->ipaddr.af != AF_UNSPEC);
652 * Otherwise look up the source IP using the same
653 * address family as the destination IP.
656 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
657 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
663 * Source isn't specified: Source is
664 * the correct address family, but all zeros.
666 memset(&home->src_ipaddr, 0, sizeof(home->src_ipaddr));
667 home->src_ipaddr.af = home->ipaddr.af;
670 if (tls && (home->proto != IPPROTO_TCP)) {
671 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available for UDP sockets.");
678 cf_log_err(cf_sectiontoitem(cs), "TLS transport is not available in this executable.");
683 * Parse the SSL client configuration.
686 home->tls = tls_client_conf_parse(tls);
694 cf_log_err(cf_sectiontoitem(cs), "Virtual home_servers cannot have a \"tls\" subsection");
699 * Make sure that this is set.
701 if (home->src_ipaddr.af == AF_UNSPEC) {
702 home->src_ipaddr.af = home->ipaddr.af;
708 if (rbtree_finddata(home_servers_byname, home) != NULL) {
709 cf_log_err(cf_sectiontoitem(cs),
710 "Duplicate home server name %s.", name2);
715 (rbtree_finddata(home_servers_byaddr, home) != NULL)) {
716 cf_log_err(cf_sectiontoitem(cs),
717 "Duplicate home server IP %s.", name2);
721 if (!rbtree_insert(home_servers_byname, home)) {
722 cf_log_err(cf_sectiontoitem(cs),
723 "Internal error %d adding home server %s.",
729 !rbtree_insert(home_servers_byaddr, home)) {
730 rbtree_deletebydata(home_servers_byname, home);
731 cf_log_err(cf_sectiontoitem(cs),
732 "Internal error %d adding home server %s.",
738 home->number = home_server_max_number++;
739 if (!rbtree_insert(home_servers_bynumber, home)) {
740 rbtree_deletebydata(home_servers_byname, home);
741 if (home->ipaddr.af != AF_UNSPEC) {
742 rbtree_deletebydata(home_servers_byname, home);
744 cf_log_err(cf_sectiontoitem(cs),
745 "Internal error %d adding home server %s.",
751 if (home->max_outstanding < 8) home->max_outstanding = 8;
752 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
754 if (home->ping_interval < 6) home->ping_interval = 6;
755 if (home->ping_interval > 120) home->ping_interval = 120;
757 if (home->response_window < 1) home->response_window = 1;
758 if (home->response_window > 60) home->response_window = 60;
759 if (home->response_window > mainconfig.max_request_time) home->response_window = mainconfig.max_request_time;
761 if (home->zombie_period < 1) home->zombie_period = 1;
762 if (home->zombie_period > 120) home->zombie_period = 120;
764 if (home->zombie_period < home->response_window) {
765 home->zombie_period = home->response_window;
768 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
769 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
771 if (home->ping_timeout < 3) home->ping_timeout = 3;
772 if (home->ping_timeout > 10) home->ping_timeout = 10;
774 if (home->revive_interval < 60) home->revive_interval = 60;
775 if (home->revive_interval > 3600) home->revive_interval = 3600;
778 if (home->coa_irt < 1) home->coa_irt = 1;
779 if (home->coa_irt > 5) home->coa_irt = 5;
781 if (home->coa_mrc < 0) home->coa_mrc = 0;
782 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
784 if (home->coa_mrt < 0) home->coa_mrt = 0;
785 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
787 if (home->coa_mrd < 5) home->coa_mrd = 5;
788 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
791 if (home->limit.max_connections > 1024) home->limit.max_connections = 1024;
795 * UDP sockets can't be connection limited.
797 if (home->proto != IPPROTO_TCP) home->limit.max_connections = 0;
800 if ((home->limit.idle_timeout > 0) && (home->limit.idle_timeout < 5))
801 home->limit.idle_timeout = 5;
802 if ((home->limit.lifetime > 0) && (home->limit.lifetime < 5))
803 home->limit.lifetime = 5;
804 if ((home->limit.lifetime > 0) && (home->limit.idle_timeout > home->limit.lifetime))
805 home->limit.idle_timeout = 0;
807 tls = cf_item_parent(cf_sectiontoitem(cs));
808 if (strcmp(cf_section_name1(tls), "server") == 0) {
809 home->parent_server = cf_section_name2(tls);
813 home_server *home2 = rad_malloc(sizeof(*home2));
815 memcpy(home2, home, sizeof(*home2));
817 home2->type = HOME_TYPE_ACCT;
819 home2->ping_user_password = NULL;
821 home2->parent_server = home->parent_server;
823 if (!rbtree_insert(home_servers_byname, home2)) {
824 cf_log_err(cf_sectiontoitem(cs),
825 "Internal error %d adding home server %s.",
832 !rbtree_insert(home_servers_byaddr, home2)) {
833 rbtree_deletebydata(home_servers_byname, home2);
834 cf_log_err(cf_sectiontoitem(cs),
835 "Internal error %d adding home server %s.",
842 home2->number = home_server_max_number++;
843 if (!rbtree_insert(home_servers_bynumber, home2)) {
844 rbtree_deletebydata(home_servers_byname, home2);
845 if (!home2->server) {
846 rbtree_deletebydata(home_servers_byname, home2);
848 cf_log_err(cf_sectiontoitem(cs),
849 "Internal error %d adding home server %s.",
858 * Mark it as already processed
860 cf_data_add(cs, "home_server", null_free, null_free);
866 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
867 int server_type, int num_home_servers)
871 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
873 if (!pool) return NULL; /* just for pairanoia */
875 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
880 pool->server_type = server_type;
881 pool->num_home_servers = num_home_servers;
886 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
887 const char *name, int server_type,
890 home_server myhome, *home;
891 CONF_SECTION *server_cs;
894 cf_log_err(cf_pairtoitem(cp),
895 "No value given for home_server.");
900 myhome.type = server_type;
901 home = rbtree_finddata(home_servers_byname, &myhome);
907 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
909 cf_log_err(cf_pairtoitem(cp),
910 "Unknown home_server \"%s\".", name);
914 home = rbtree_finddata(home_servers_byname, &myhome);
916 cf_log_err(cf_pairtoitem(cp),
917 "Internal error %d adding home server \"%s\".",
927 static int server_pool_add(realm_config_t *rc,
928 CONF_SECTION *cs, int server_type, int do_print)
931 home_pool_t *pool = NULL;
934 int num_home_servers;
937 name2 = cf_section_name1(cs);
938 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
939 (strcasecmp(name2, "home_server_pool") != 0))) {
940 cf_log_err(cf_sectiontoitem(cs),
941 "Section is not a home_server_pool.");
945 name2 = cf_section_name2(cs);
947 cf_log_err(cf_sectiontoitem(cs),
948 "Server pool section is missing a name.");
953 * Count the home servers and initalize them.
955 num_home_servers = 0;
956 for (cp = cf_pair_find(cs, "home_server");
958 cp = cf_pair_find_next(cs, cp, "home_server")) {
961 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
962 server_type, &home)) {
967 if (num_home_servers == 0) {
968 cf_log_err(cf_sectiontoitem(cs),
969 "No home servers defined in pool %s",
974 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
980 * Fallback servers must be defined, and must be
983 cp = cf_pair_find(cs, "fallback");
986 if (server_type == HOME_TYPE_COA) {
987 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
992 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
993 server_type, &pool->fallback)) {
998 if (!pool->fallback->server) {
999 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
1004 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
1006 cp = cf_pair_find(cs, "type");
1008 static FR_NAME_NUMBER pool_types[] = {
1009 { "load-balance", HOME_POOL_LOAD_BALANCE },
1010 { "fail-over", HOME_POOL_FAIL_OVER },
1011 { "round_robin", HOME_POOL_LOAD_BALANCE },
1012 { "fail_over", HOME_POOL_FAIL_OVER },
1013 { "client-balance", HOME_POOL_CLIENT_BALANCE },
1014 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
1015 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
1019 value = cf_pair_value(cp);
1021 cf_log_err(cf_pairtoitem(cp),
1022 "No value given for type.");
1026 pool->type = fr_str2int(pool_types, value, 0);
1028 cf_log_err(cf_pairtoitem(cp),
1029 "Unknown type \"%s\".",
1034 if (do_print) cf_log_info(cs, "\ttype = %s", value);
1037 cp = cf_pair_find(cs, "virtual_server");
1039 pool->virtual_server = cf_pair_value(cp);
1040 if (!pool->virtual_server) {
1041 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
1046 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
1049 if (!cf_section_sub_find_name2(rc->cs, "server",
1050 pool->virtual_server)) {
1051 cf_log_err(cf_pairtoitem(cp), "No such server %s",
1052 pool->virtual_server);
1058 num_home_servers = 0;
1059 for (cp = cf_pair_find(cs, "home_server");
1061 cp = cf_pair_find_next(cs, cp, "home_server")) {
1064 value = cf_pair_value(cp);
1066 memset(&myhome, 0, sizeof(myhome));
1067 myhome.name = value;
1068 myhome.type = server_type;
1070 home = rbtree_finddata(home_servers_byname, &myhome);
1072 DEBUG2("Internal sanity check failed");
1077 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
1081 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
1082 pool->servers[num_home_servers++] = home;
1083 } /* loop over home_server's */
1085 if (pool->fallback && do_print) {
1086 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
1089 if (!rbtree_insert(home_pools_byname, pool)) {
1090 rad_assert("Internal sanity check failed");
1094 if (do_print) cf_log_info(cs, " }");
1096 cf_data_add(cs, "home_server_pool", pool, free);
1098 rad_assert(pool->server_type != 0);
1103 if (do_print) cf_log_info(cs, " }");
1109 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
1111 const char *name, const char *secret,
1112 home_pool_type_t ldflag, home_pool_t **pool_p,
1113 int type, const char *server)
1116 int i, insert_point, num_home_servers;
1117 home_server myhome, *home;
1118 home_pool_t mypool, *pool;
1119 CONF_SECTION *subcs;
1121 rc = rc; /* -Wunused */
1130 * LOCAL realms get sanity checked, and nothing else happens.
1132 if (strcmp(name, "LOCAL") == 0) {
1134 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
1141 return 0; /* Not proxying. Can't do non-LOCAL realms */
1144 mypool.name = realm;
1145 mypool.server_type = type;
1146 pool = rbtree_finddata(home_pools_byname, &mypool);
1148 if (pool->type != ldflag) {
1149 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
1153 if (pool->server_type != type) {
1154 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1161 home = rbtree_finddata(home_servers_byname, &myhome);
1163 if (secret && (strcmp(home->secret, secret) != 0)) {
1164 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1168 if (home->type != type) {
1169 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1174 * See if the home server is already listed
1175 * in the pool. If so, do nothing else.
1177 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1178 if (pool->servers[i] == home) {
1185 * If we do have a pool, check that there is room to
1186 * insert the home server we've found, or the one that we
1189 * Note that we insert it into the LAST available
1190 * position, in order to maintain the same order as in
1191 * the configuration files.
1195 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1196 if (pool->servers[i]) break;
1198 if (!pool->servers[i]) {
1203 if (insert_point < 0) {
1204 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);
1210 * No home server, allocate one.
1216 home = rad_malloc(sizeof(*home));
1217 memset(home, 0, sizeof(*home));
1220 home->hostname = name;
1222 home->secret = secret;
1224 home->proto = IPPROTO_UDP;
1226 p = strchr(name, ':');
1228 if (type == HOME_TYPE_AUTH) {
1229 home->port = PW_AUTH_UDP_PORT;
1231 home->port = PW_ACCT_UDP_PORT;
1237 } else if (p == name) {
1238 cf_log_err(cf_sectiontoitem(cs),
1239 "Invalid hostname %s.",
1245 home->port = atoi(p + 1);
1246 if ((home->port == 0) || (home->port > 65535)) {
1247 cf_log_err(cf_sectiontoitem(cs),
1254 q = rad_malloc((p - name) + 1);
1255 memcpy(q, name, (p - name));
1261 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1262 cf_log_err(cf_sectiontoitem(cs),
1263 "Failed looking up hostname %s.",
1269 home->src_ipaddr.af = home->ipaddr.af;
1271 home->ipaddr.af = AF_UNSPEC;
1272 home->server = server;
1277 * Use the old-style configuration.
1279 home->max_outstanding = 65535*16;
1280 home->zombie_period = rc->retry_delay * rc->retry_count;
1281 if (home->zombie_period == 0) home->zombie_period =30;
1282 home->response_window = home->zombie_period - 1;
1284 home->ping_check = HOME_PING_CHECK_NONE;
1286 home->revive_interval = rc->dead_time;
1288 if (rbtree_finddata(home_servers_byaddr, home)) {
1289 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1294 if (!rbtree_insert(home_servers_byname, home)) {
1295 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1300 if (!rbtree_insert(home_servers_byaddr, home)) {
1301 rbtree_deletebydata(home_servers_byname, home);
1302 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1308 home->number = home_server_max_number++;
1309 if (!rbtree_insert(home_servers_bynumber, home)) {
1310 rbtree_deletebydata(home_servers_byname, home);
1311 if (home->ipaddr.af != AF_UNSPEC) {
1312 rbtree_deletebydata(home_servers_byname, home);
1314 cf_log_err(cf_sectiontoitem(cs),
1315 "Internal error %d adding home server %s.",
1324 * We now have a home server, see if we can insert it
1325 * into pre-existing pool.
1327 if (insert_point >= 0) {
1328 rad_assert(pool != NULL);
1329 pool->servers[insert_point] = home;
1333 rad_assert(pool == NULL);
1334 rad_assert(home != NULL);
1337 * Count the old-style realms of this name.
1339 num_home_servers = 0;
1340 for (subcs = cf_section_find_next(cs, NULL, "realm");
1342 subcs = cf_section_find_next(cs, subcs, "realm")) {
1343 const char *this = cf_section_name2(subcs);
1345 if (!this || (strcmp(this, realm) != 0)) continue;
1349 if (num_home_servers == 0) {
1350 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1355 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1358 pool->servers[0] = home;
1360 if (!rbtree_insert(home_pools_byname, pool)) {
1361 rad_assert("Internal sanity check failed");
1371 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1374 const char *secret = NULL;
1375 home_pool_type_t ldflag;
1378 cp = cf_pair_find(cs, "ldflag");
1379 ldflag = HOME_POOL_FAIL_OVER;
1381 host = cf_pair_value(cp);
1383 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1387 if (strcasecmp(host, "fail_over") == 0) {
1388 cf_log_info(cs, "\tldflag = fail_over");
1390 } else if (strcasecmp(host, "round_robin") == 0) {
1391 ldflag = HOME_POOL_LOAD_BALANCE;
1392 cf_log_info(cs, "\tldflag = round_robin");
1395 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1398 } /* else don't print it. */
1401 * Allow old-style if it doesn't exist, or if it exists and
1404 cp = cf_pair_find(cs, "authhost");
1406 host = cf_pair_value(cp);
1408 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1412 if (strcmp(host, "LOCAL") != 0) {
1413 cp = cf_pair_find(cs, "secret");
1415 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1419 secret = cf_pair_value(cp);
1421 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1426 cf_log_info(cs, "\tauthhost = %s", host);
1428 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1429 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1434 cp = cf_pair_find(cs, "accthost");
1436 host = cf_pair_value(cp);
1438 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1443 * Don't look for a secret again if it was found
1446 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1447 cp = cf_pair_find(cs, "secret");
1449 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1453 secret = cf_pair_value(cp);
1455 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1460 cf_log_info(cs, "\taccthost = %s", host);
1462 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1463 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1468 cp = cf_pair_find(cs, "virtual_server");
1470 host = cf_pair_value(cp);
1472 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1476 cf_log_info(cs, "\tvirtual_server = %s", host);
1478 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1479 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1482 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1483 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1488 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1496 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1497 const char *name, home_pool_t **dest,
1498 int server_type, int do_print)
1500 home_pool_t mypool, *pool;
1503 mypool.server_type = server_type;
1505 pool = rbtree_finddata(home_pools_byname, &mypool);
1507 CONF_SECTION *pool_cs;
1509 pool_cs = cf_section_sub_find_name2(rc->cs,
1513 pool_cs = cf_section_sub_find_name2(rc->cs,
1518 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1522 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1526 pool = rbtree_finddata(home_pools_byname, &mypool);
1528 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1533 if (pool->server_type != server_type) {
1534 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1545 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1551 home_pool_t *auth_pool, *acct_pool;
1552 const char *auth_pool_name, *acct_pool_name;
1554 const char *coa_pool_name;
1555 home_pool_t *coa_pool;
1559 name2 = cf_section_name1(cs);
1560 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1561 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1565 name2 = cf_section_name2(cs);
1567 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1572 auth_pool = acct_pool = NULL;
1573 auth_pool_name = acct_pool_name = NULL;
1576 coa_pool_name = NULL;
1580 * Prefer new configuration to old one.
1582 cp = cf_pair_find(cs, "pool");
1583 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1584 if (cp) auth_pool_name = cf_pair_value(cp);
1585 if (cp && auth_pool_name) {
1586 acct_pool_name = auth_pool_name;
1587 if (!add_pool_to_realm(rc, cs,
1588 auth_pool_name, &auth_pool,
1589 HOME_TYPE_AUTH, 1)) {
1592 if (!add_pool_to_realm(rc, cs,
1593 auth_pool_name, &acct_pool,
1594 HOME_TYPE_ACCT, 0)) {
1599 cp = cf_pair_find(cs, "auth_pool");
1600 if (cp) auth_pool_name = cf_pair_value(cp);
1601 if (cp && auth_pool_name) {
1603 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1606 if (!add_pool_to_realm(rc, cs,
1607 auth_pool_name, &auth_pool,
1608 HOME_TYPE_AUTH, 1)) {
1613 cp = cf_pair_find(cs, "acct_pool");
1614 if (cp) acct_pool_name = cf_pair_value(cp);
1615 if (cp && acct_pool_name) {
1616 int do_print = TRUE;
1619 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1625 (strcmp(auth_pool_name, acct_pool_name) != 0))) {
1629 if (!add_pool_to_realm(rc, cs,
1630 acct_pool_name, &acct_pool,
1631 HOME_TYPE_ACCT, do_print)) {
1637 cp = cf_pair_find(cs, "coa_pool");
1638 if (cp) coa_pool_name = cf_pair_value(cp);
1639 if (cp && coa_pool_name) {
1640 int do_print = TRUE;
1642 if (!add_pool_to_realm(rc, cs,
1643 coa_pool_name, &coa_pool,
1644 HOME_TYPE_COA, do_print)) {
1651 cf_log_info(cs, " realm %s {", name2);
1655 * The realm MAY already exist if it's an old-style realm.
1656 * In that case, merge the old-style realm with this one.
1658 r = realm_find2(name2);
1659 if (r && (strcmp(r->name, name2) == 0)) {
1660 if (cf_pair_find(cs, "auth_pool") ||
1661 cf_pair_find(cs, "acct_pool")) {
1662 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1666 if (!old_realm_config(rc, cs, r)) {
1670 cf_log_info(cs, " } # realm %s", name2);
1676 if (name2[0] == '~') {
1681 * Include substring matches.
1683 rcode = regcomp(®, name2 + 1,
1684 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1688 regerror(rcode, ®, buffer, sizeof(buffer));
1690 cf_log_err(cf_sectiontoitem(cs),
1691 "Invalid regex \"%s\": %s",
1699 r = rad_malloc(sizeof(*r));
1700 memset(r, 0, sizeof(*r));
1705 r->auth_pool = auth_pool;
1706 r->acct_pool = acct_pool;
1708 r->coa_pool = coa_pool;
1711 if (auth_pool_name &&
1712 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1713 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1715 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1716 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1718 if (coa_pool_name) cf_log_info(cs, "\tcoa_pool = %s", coa_pool_name);
1723 cp = cf_pair_find(cs, "nostrip");
1724 if (cp && (cf_pair_value(cp) == NULL)) {
1726 cf_log_info(cs, "\tnostrip");
1730 * We're a new-style realm. Complain if we see the old
1733 if (r->auth_pool || r->acct_pool) {
1734 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1735 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1736 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1737 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1738 DEBUG2W("Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1743 * The realm MAY be an old-style realm, as there
1744 * was no auth_pool or acct_pool. Double-check
1745 * it, just to be safe.
1747 } else if (!old_realm_config(rc, cs, r)) {
1753 * It's a regex. Add it to a separate list.
1755 if (name2[0] == '~') {
1756 realm_regex_t *rr, **last;
1758 rr = rad_malloc(sizeof(*rr));
1760 last = &realms_regex;
1761 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1769 cf_log_info(cs, " }");
1774 if (!rbtree_insert(realms_byname, r)) {
1775 rad_assert("Internal sanity check failed");
1779 cf_log_info(cs, " }");
1784 cf_log_info(cs, " } # realm %s", name2);
1790 static const FR_NAME_NUMBER home_server_types[] = {
1791 { "auth", HOME_TYPE_AUTH },
1792 { "auth+acct", HOME_TYPE_AUTH },
1793 { "acct", HOME_TYPE_ACCT },
1794 { "coa", HOME_TYPE_COA },
1798 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1801 const char *name, *type;
1803 CONF_SECTION *server_cs;
1805 cp = cf_pair_find(cs, "home_server");
1807 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1808 return HOME_TYPE_INVALID;
1811 name = cf_pair_value(cp);
1813 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1814 return HOME_TYPE_INVALID;
1817 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1819 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1820 return HOME_TYPE_INVALID;
1823 cp = cf_pair_find(server_cs, "type");
1825 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1826 return HOME_TYPE_INVALID;
1829 type = cf_pair_value(cp);
1831 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1832 return HOME_TYPE_INVALID;
1835 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1836 if (home == HOME_TYPE_INVALID) {
1837 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1838 return HOME_TYPE_INVALID;
1841 return home; /* 'cause we miss it so much */
1845 int realms_init(CONF_SECTION *config)
1849 CONF_SECTION *server_cs;
1851 realm_config_t *rc, *old_rc;
1853 if (realms_byname) return 1;
1855 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1856 if (!realms_byname) {
1862 home_servers_byaddr = rbtree_create(home_server_addr_cmp, home_server_free, 0);
1863 if (!home_servers_byaddr) {
1868 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1869 if (!home_servers_byname) {
1875 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1876 if (!home_servers_bynumber) {
1882 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1883 if (!home_pools_byname) {
1889 rc = rad_malloc(sizeof(*rc));
1890 memset(rc, 0, sizeof(*rc));
1894 cs = cf_subsection_find_next(config, NULL, "proxy");
1896 if (cf_section_parse(cs, rc, proxy_config) < 0) {
1897 radlog(L_ERR, "Failed parsing proxy section");
1904 rc->dead_time = DEAD_TIME;
1905 rc->retry_count = RETRY_COUNT;
1906 rc->retry_delay = RETRY_DELAY;
1908 rc->wake_all_if_all_dead= 0;
1911 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1913 cs = cf_subsection_find_next(config, cs, "home_server")) {
1914 if (!home_server_add(rc, cs)) {
1922 * Loop over virtual servers to find homes which are
1925 for (server_cs = cf_subsection_find_next(config, NULL, "server");
1927 server_cs = cf_subsection_find_next(config, server_cs, "server")) {
1928 for (cs = cf_subsection_find_next(server_cs, NULL, "home_server");
1930 cs = cf_subsection_find_next(server_cs, cs, "home_server")) {
1931 if (!home_server_add(rc, cs)) {
1940 for (cs = cf_subsection_find_next(config, NULL, "realm");
1942 cs = cf_subsection_find_next(config, cs, "realm")) {
1943 if (!realm_add(rc, cs)) {
1952 * CoA pools aren't necessarily tied to realms.
1954 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1956 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1960 * Pool was already loaded.
1962 if (cf_data_find(cs, "home_server_pool")) continue;
1964 type = pool_peek_type(config, cs);
1965 if (type == HOME_TYPE_INVALID) {
1971 if (!server_pool_add(rc, cs, type, TRUE)) {
1981 xlat_register("home_server", xlat_home_server, NULL);
1982 xlat_register("home_server_pool", xlat_server_pool, NULL);
1986 * Swap pointers atomically.
1988 old_rc = realm_config;
1996 * Find a realm where "name" might be the regex.
1998 REALM *realm_find2(const char *name)
2003 if (!name) name = "NULL";
2005 myrealm.name = name;
2006 realm = rbtree_finddata(realms_byname, &myrealm);
2007 if (realm) return realm;
2011 realm_regex_t *this;
2013 for (this = realms_regex; this != NULL; this = this->next) {
2014 if (strcmp(this->realm->name, name) == 0) {
2022 * Couldn't find a realm. Look for DEFAULT.
2024 myrealm.name = "DEFAULT";
2025 return rbtree_finddata(realms_byname, &myrealm);
2030 * Find a realm in the REALM list.
2032 REALM *realm_find(const char *name)
2037 if (!name) name = "NULL";
2039 myrealm.name = name;
2040 realm = rbtree_finddata(realms_byname, &myrealm);
2041 if (realm) return realm;
2045 realm_regex_t *this;
2047 for (this = realms_regex; this != NULL; this = this->next) {
2052 * Include substring matches.
2054 if (regcomp(®, this->realm->name + 1,
2055 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
2059 compare = regexec(®, name, 0, NULL, 0);
2062 if (compare == 0) return this->realm;
2068 * Couldn't find a realm. Look for DEFAULT.
2070 myrealm.name = "DEFAULT";
2071 return rbtree_finddata(realms_byname, &myrealm);
2078 * Allocate the proxy list if it doesn't already exist, and copy request
2079 * VPs into it. Setup src/dst IP addresses based on home server, and
2080 * calculate and add the message-authenticator.
2082 * This is a distinct function from home_server_ldb, as not all home_server
2083 * lookups result in the *CURRENT* request being proxied,
2084 * as in rlm_replicate, and this may trigger asserts elsewhere in the
2087 void home_server_update_request(home_server *home, REQUEST *request)
2091 * Allocate the proxy packet, only if it wasn't
2092 * already allocated by a module. This check is
2093 * mainly to support the proxying of EAP-TTLS and
2094 * EAP-PEAP tunneled requests.
2096 * In those cases, the EAP module creates a
2097 * "fake" request, and recursively passes it
2098 * through the authentication stage of the
2099 * server. The module then checks if the request
2100 * was supposed to be proxied, and if so, creates
2101 * a proxy packet from the TUNNELED request, and
2102 * not from the EAP request outside of the
2105 * The proxy then works like normal, except that
2106 * the response packet is "eaten" by the EAP
2107 * module, and encapsulated into an EAP packet.
2109 if (!request->proxy) {
2110 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2111 radlog(L_ERR, "no memory");
2116 * Copy the request, then look up name
2117 * and plain-text password in the copy.
2119 * Note that the User-Name attribute is
2120 * the *original* as sent over by the
2121 * client. The Stripped-User-Name
2122 * attribute is the one hacked through
2125 request->proxy->vps = paircopy(request->packet->vps);
2129 * Update the various fields as appropriate.
2131 request->proxy->src_ipaddr = home->src_ipaddr;
2132 request->proxy->src_port = 0;
2133 request->proxy->dst_ipaddr = home->ipaddr;
2134 request->proxy->dst_port = home->port;
2135 request->home_server = home;
2138 * Access-Requests have a Message-Authenticator added,
2139 * unless one already exists.
2141 if ((request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2142 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR, 0, TAG_ANY)) {
2143 radius_pairmake(request, &request->proxy->vps,
2144 "Message-Authenticator", "0x00",
2149 home_server *home_server_ldb(const char *realmname,
2150 home_pool_t *pool, REQUEST *request)
2154 home_server *found = NULL;
2155 home_server *zombie = NULL;
2159 * Determine how to pick choose the home server.
2161 switch (pool->type) {
2165 * For load-balancing by client IP address, we
2166 * pick a home server by hashing the client IP.
2168 * This isn't as even a load distribution as
2169 * tracking the State attribute, but it's better
2172 case HOME_POOL_CLIENT_BALANCE:
2173 switch (request->packet->src_ipaddr.af) {
2175 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2176 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2179 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2180 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2186 start = hash % pool->num_home_servers;
2189 case HOME_POOL_CLIENT_PORT_BALANCE:
2190 switch (request->packet->src_ipaddr.af) {
2192 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
2193 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
2196 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
2197 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
2203 fr_hash_update(&request->packet->src_port,
2204 sizeof(request->packet->src_port), hash);
2205 start = hash % pool->num_home_servers;
2208 case HOME_POOL_KEYED_BALANCE:
2209 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY, 0, TAG_ANY)) != NULL) {
2210 hash = fr_hash(vp->vp_strvalue, vp->length);
2211 start = hash % pool->num_home_servers;
2216 case HOME_POOL_LOAD_BALANCE:
2217 case HOME_POOL_FAIL_OVER:
2221 default: /* this shouldn't happen... */
2228 * Starting with the home server we chose, loop through
2229 * all home servers. If the current one is dead, skip
2230 * it. If it is too busy, skip it.
2232 * Otherwise, use it.
2234 for (count = 0; count < pool->num_home_servers; count++) {
2235 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
2237 if (!home) continue;
2240 * Skip dead home servers.
2242 * Home servers that are unknown, alive, or zombie
2243 * are used for proxying.
2245 if (home->state == HOME_STATE_IS_DEAD) {
2250 * This home server is too busy. Choose another one.
2252 if (home->currently_outstanding >= home->max_outstanding) {
2258 * We read the packet from a detail file, AND it
2259 * came from this server. Don't re-proxy it
2262 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
2263 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
2264 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
2270 * Default virtual: ignore homes tied to a
2273 if (!request->server && home->parent_server) {
2278 * A virtual AND home is tied to virtual,
2279 * ignore ones which don't match.
2281 if (request->server && home->parent_server &&
2282 strcmp(request->server, home->parent_server) != 0) {
2287 * Allow request->server && !home->parent_server
2289 * i.e. virtuals can proxy to globally defined
2294 * It's zombie, so we remember the first zombie
2295 * we find, but we don't mark it as a "live"
2298 if (home->state == HOME_STATE_ZOMBIE) {
2299 if (!zombie) zombie = home;
2304 * We've found the first "live" one. Use that.
2306 if (pool->type != HOME_POOL_LOAD_BALANCE) {
2312 * Otherwise we're doing some kind of load balancing.
2313 * If we haven't found one yet, pick this one.
2320 RDEBUG3("PROXY %s %d\t%s %d",
2321 found->name, found->currently_outstanding,
2322 home->name, home->currently_outstanding);
2325 * Prefer this server if it's less busy than the
2326 * one we had previously found.
2328 if (home->currently_outstanding < found->currently_outstanding) {
2329 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2330 home->name, found->name);
2336 * Ignore servers which are busier than the one
2339 if (home->currently_outstanding > found->currently_outstanding) {
2340 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2341 home->name, found->name);
2346 * From the list of servers which have the same
2347 * load, choose one at random.
2349 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2352 } /* loop over the home servers */
2355 * We have no live servers, BUT we have a zombie. Use
2356 * the zombie as a last resort.
2358 if (!found && zombie) {
2364 * There's a fallback if they're all dead.
2366 if (!found && pool->fallback) {
2367 found = pool->fallback;
2369 DEBUGW("Home server pool %s failing over to fallback %s",
2370 pool->name, found->server);
2371 if (pool->in_fallback) goto update_and_return;
2373 pool->in_fallback = TRUE;
2376 * Run the trigger once an hour saying that
2379 if ((pool->time_all_dead + 3600) < request->timestamp) {
2380 pool->time_all_dead = request->timestamp;
2381 exec_trigger(request, pool->cs, "home_server_pool.fallback", FALSE);
2387 if ((found != pool->fallback) && pool->in_fallback) {
2388 pool->in_fallback = FALSE;
2389 exec_trigger(request, pool->cs, "home_server_pool.normal", FALSE);
2396 * No live match found, and no fallback to the "DEFAULT"
2397 * realm. We fix this by blindly marking all servers as
2398 * "live". But only do it for ones that don't support
2399 * "pings", as they will be marked live when they
2400 * actually are live.
2402 if (!realm_config->fallback &&
2403 realm_config->wake_all_if_all_dead) {
2404 for (count = 0; count < pool->num_home_servers; count++) {
2405 home_server *home = pool->servers[count];
2407 if (!home) continue;
2409 if ((home->state == HOME_STATE_IS_DEAD) &&
2410 (home->ping_check == HOME_PING_CHECK_NONE)) {
2411 home->state = HOME_STATE_ALIVE;
2412 if (!found) found = home;
2416 if (found) goto update_and_return;
2420 * Still nothing. Look up the DEFAULT realm, but only
2421 * if we weren't looking up the NULL or DEFAULT realms.
2423 if (realm_config->fallback &&
2425 (strcmp(realmname, "NULL") != 0) &&
2426 (strcmp(realmname, "DEFAULT") != 0)) {
2427 REALM *rd = realm_find("DEFAULT");
2429 if (!rd) return NULL;
2432 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2433 pool = rd->auth_pool;
2435 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2436 pool = rd->acct_pool;
2438 if (!pool) return NULL;
2440 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2441 return home_server_ldb(rd->name, pool, request);
2445 * Still haven't found anything. Oh well.
2451 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port, int proto)
2455 memset(&myhome, 0, sizeof(myhome));
2456 myhome.ipaddr = *ipaddr;
2457 myhome.src_ipaddr.af = ipaddr->af;
2460 myhome.proto = proto;
2462 myhome.proto = IPPROTO_UDP;
2464 myhome.server = NULL; /* we're not called for internal proxying */
2466 return rbtree_finddata(home_servers_byaddr, &myhome);
2470 home_server *home_server_byname(const char *name, int type)
2474 memset(&myhome, 0, sizeof(myhome));
2478 return rbtree_finddata(home_servers_byname, &myhome);
2483 home_server *home_server_bynumber(int number)
2487 memset(&myhome, 0, sizeof(myhome));
2488 myhome.number = number;
2489 myhome.server = NULL; /* we're not called for internal proxying */
2491 return rbtree_finddata(home_servers_bynumber, &myhome);
2495 home_pool_t *home_pool_byname(const char *name, int type)
2499 memset(&mypool, 0, sizeof(mypool));
2501 mypool.server_type = type;
2502 return rbtree_finddata(home_pools_byname, &mypool);