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>
39 * For POSIX Regular expressions.
40 * (0) Means no extended regular expressions.
41 * REG_EXTENDED means use extended regular expressions.
44 #define REG_EXTENDED (0)
56 static rbtree_t *realms_byname = NULL;
59 typedef struct realm_regex_t {
61 struct realm_regex_t *next;
64 static realm_regex_t *realms_regex = NULL;
66 #endif /* HAVE_REGEX_H */
68 typedef struct realm_config_t {
74 int wake_all_if_all_dead;
77 static realm_config_t *realm_config = NULL;
80 static rbtree_t *home_servers_byaddr = NULL;
81 static rbtree_t *home_servers_byname = NULL;
83 static int home_server_max_number = 0;
84 static rbtree_t *home_servers_bynumber = NULL;
87 static rbtree_t *home_pools_byname = NULL;
90 * Map the proxy server configuration parameters to variables.
92 static const CONF_PARSER proxy_config[] = {
93 { "retry_delay", PW_TYPE_INTEGER,
94 offsetof(realm_config_t, retry_delay),
95 NULL, Stringify(RETRY_DELAY) },
97 { "retry_count", PW_TYPE_INTEGER,
98 offsetof(realm_config_t, retry_count),
99 NULL, Stringify(RETRY_COUNT) },
101 { "default_fallback", PW_TYPE_BOOLEAN,
102 offsetof(realm_config_t, fallback),
105 { "dead_time", PW_TYPE_INTEGER,
106 offsetof(realm_config_t, dead_time),
107 NULL, Stringify(DEAD_TIME) },
109 { "wake_all_if_all_dead", PW_TYPE_BOOLEAN,
110 offsetof(realm_config_t, wake_all_if_all_dead),
113 #ifdef WITH_POST_PROXY_AUTHORIZE
114 { "post_proxy_authorize", PW_TYPE_BOOLEAN, 0,
115 &mainconfig.post_proxy_authorize, "yes" },
118 { NULL, -1, 0, NULL, NULL }
122 static int realm_name_cmp(const void *one, const void *two)
124 const REALM *a = one;
125 const REALM *b = two;
127 return strcasecmp(a->name, b->name);
132 static int home_server_name_cmp(const void *one, const void *two)
134 const home_server *a = one;
135 const home_server *b = two;
137 if (a->type < b->type) return -1;
138 if (a->type > b->type) return +1;
140 return strcasecmp(a->name, b->name);
143 static int home_server_addr_cmp(const void *one, const void *two)
145 const home_server *a = one;
146 const home_server *b = two;
148 if (a->server && !b->server) return -1;
149 if (!a->server && b->server) return +1;
150 if (a->server && b->server) {
151 int rcode = a->type - b->type;
152 if (rcode != 0) return rcode;
153 return strcmp(a->server, b->server);
156 if (a->port < b->port) return -1;
157 if (a->port > b->port) return +1;
159 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
163 static int home_server_number_cmp(const void *one, const void *two)
165 const home_server *a = one;
166 const home_server *b = two;
168 return (a->number - b->number);
172 static int home_pool_name_cmp(const void *one, const void *two)
174 const home_pool_t *a = one;
175 const home_pool_t *b = two;
177 if (a->server_type < b->server_type) return -1;
178 if (a->server_type > b->server_type) return +1;
180 return strcasecmp(a->name, b->name);
185 * Xlat for %{home_server:foo}
187 static size_t xlat_home_server(UNUSED void *instance, REQUEST *request,
188 char *fmt, char *out, size_t outlen,
189 UNUSED RADIUS_ESCAPE_STRING func)
191 const char *value = NULL;
194 if (!fmt || !out || (outlen < 1)) return 0;
196 if (!request || !request->home_server) {
201 cp = cf_pair_find(request->home_server->cs, fmt);
202 if (!cp || !(value = cf_pair_value(cp))) {
207 strlcpy(out, value, outlen);
214 * Xlat for %{home_server_pool:foo}
216 static size_t xlat_server_pool(UNUSED void *instance, REQUEST *request,
217 char *fmt, char *out, size_t outlen,
218 UNUSED RADIUS_ESCAPE_STRING func)
220 const char *value = NULL;
223 if (!fmt || !out || (outlen < 1)) return 0;
225 if (!request || !request->home_pool) {
230 cp = cf_pair_find(request->home_pool->cs, fmt);
231 if (!cp || !(value = cf_pair_value(cp))) {
236 strlcpy(out, value, outlen);
242 void realms_free(void)
246 rbtree_free(home_servers_bynumber);
247 home_servers_bynumber = NULL;
250 rbtree_free(home_servers_byname);
251 home_servers_byname = NULL;
253 rbtree_free(home_servers_byaddr);
254 home_servers_byaddr = NULL;
256 rbtree_free(home_pools_byname);
257 home_pools_byname = NULL;
260 rbtree_free(realms_byname);
261 realms_byname = NULL;
265 realm_regex_t *this, *next;
267 for (this = realms_regex; this != NULL; this = next) {
282 static struct in_addr hs_ip4addr;
283 static struct in6_addr hs_ip6addr;
284 static char *hs_srcipaddr = NULL;
285 static char *hs_type = NULL;
286 static char *hs_check = NULL;
287 static char *hs_virtual_server = NULL;
289 static CONF_PARSER home_server_config[] = {
290 { "ipaddr", PW_TYPE_IPADDR,
291 0, &hs_ip4addr, NULL },
292 { "ipv6addr", PW_TYPE_IPV6ADDR,
293 0, &hs_ip6addr, NULL },
294 { "virtual_server", PW_TYPE_STRING_PTR,
295 0, &hs_virtual_server, NULL },
297 { "port", PW_TYPE_INTEGER,
298 offsetof(home_server,port), NULL, "0" },
300 { "type", PW_TYPE_STRING_PTR,
303 { "secret", PW_TYPE_STRING_PTR,
304 offsetof(home_server,secret), NULL, NULL},
306 { "src_ipaddr", PW_TYPE_STRING_PTR,
307 0, &hs_srcipaddr, NULL },
309 { "response_window", PW_TYPE_INTEGER,
310 offsetof(home_server,response_window), NULL, "30" },
311 { "no_response_fail", PW_TYPE_BOOLEAN,
312 offsetof(home_server,no_response_fail), NULL, NULL },
313 { "max_outstanding", PW_TYPE_INTEGER,
314 offsetof(home_server,max_outstanding), NULL, "65536" },
315 { "require_message_authenticator", PW_TYPE_BOOLEAN,
316 offsetof(home_server, message_authenticator), 0, NULL },
318 { "zombie_period", PW_TYPE_INTEGER,
319 offsetof(home_server,zombie_period), NULL, "40" },
320 { "status_check", PW_TYPE_STRING_PTR,
321 0, &hs_check, "none" },
322 { "ping_check", PW_TYPE_STRING_PTR,
323 0, &hs_check, NULL },
325 { "ping_interval", PW_TYPE_INTEGER,
326 offsetof(home_server,ping_interval), NULL, "30" },
327 { "check_interval", PW_TYPE_INTEGER,
328 offsetof(home_server,ping_interval), NULL, "30" },
329 { "num_answers_to_alive", PW_TYPE_INTEGER,
330 offsetof(home_server,num_pings_to_alive), NULL, "3" },
331 { "num_pings_to_alive", PW_TYPE_INTEGER,
332 offsetof(home_server,num_pings_to_alive), NULL, "3" },
333 { "revive_interval", PW_TYPE_INTEGER,
334 offsetof(home_server,revive_interval), NULL, "300" },
335 { "status_check_timeout", PW_TYPE_INTEGER,
336 offsetof(home_server,ping_timeout), NULL, "4" },
338 { "username", PW_TYPE_STRING_PTR,
339 offsetof(home_server,ping_user_name), NULL, NULL},
340 { "password", PW_TYPE_STRING_PTR,
341 offsetof(home_server,ping_user_password), NULL, NULL},
344 { "historic_average_window", PW_TYPE_INTEGER,
345 offsetof(home_server,ema.window), NULL, NULL },
349 { "irt", PW_TYPE_INTEGER,
350 offsetof(home_server, coa_irt), 0, Stringify(2) },
351 { "mrt", PW_TYPE_INTEGER,
352 offsetof(home_server, coa_mrt), 0, Stringify(16) },
353 { "mrc", PW_TYPE_INTEGER,
354 offsetof(home_server, coa_mrc), 0, Stringify(5) },
355 { "mrd", PW_TYPE_INTEGER,
356 offsetof(home_server, coa_mrd), 0, Stringify(30) },
359 { NULL, -1, 0, NULL, NULL } /* end the list */
363 static void null_free(UNUSED void *data)
367 static int home_server_add(realm_config_t *rc, CONF_SECTION *cs)
374 free(hs_virtual_server); /* used only for printing during parsing */
375 hs_virtual_server = NULL;
377 name2 = cf_section_name2(cs);
379 cf_log_err(cf_sectiontoitem(cs),
380 "Home server section is missing a name.");
384 home = rad_malloc(sizeof(*home));
385 memset(home, 0, sizeof(*home));
391 * For zombie period calculations. We want to count
392 * zombies from the time when the server starts, instead
395 home->last_packet = time(NULL);
398 * Authentication servers have a default "no_response_fail = 0".
399 * Accounting servers have a default "no_response_fail = 1".
401 * This is because authentication packets are retried, so
402 * they can fail over to another home server. Accounting
403 * packets are not retried, so they cannot fail over, and
404 * instead should be rejected immediately.
406 home->no_response_fail = 2;
408 memset(&hs_ip4addr, 0, sizeof(hs_ip4addr));
409 memset(&hs_ip6addr, 0, sizeof(hs_ip6addr));
410 if (cf_section_parse(cs, home, home_server_config) < 0) {
416 * Figure out which one to use.
418 if (cf_pair_find(cs, "ipaddr")) {
419 home->ipaddr.af = AF_INET;
420 home->ipaddr.ipaddr.ip4addr = hs_ip4addr;
422 } else if (cf_pair_find(cs, "ipv6addr")) {
423 home->ipaddr.af = AF_INET6;
424 home->ipaddr.ipaddr.ip6addr = hs_ip6addr;
426 } else if ((cp = cf_pair_find(cs, "virtual_server")) != NULL) {
427 home->ipaddr.af = AF_UNSPEC;
428 home->server = cf_pair_value(cp);
430 cf_log_err(cf_sectiontoitem(cs),
431 "Invalid value for virtual_server");
435 if (!cf_section_sub_find_name2(rc->cs, "server", home->server)) {
437 cf_log_err(cf_sectiontoitem(cs),
438 "No such server %s", home->server);
443 * When CoA is used, the user has to specify the type
444 * of the home server, even when they point to
447 home->secret = strdup("");
451 cf_log_err(cf_sectiontoitem(cs),
452 "No ipaddr, ipv6addr, or virtual_server defined for home server \"%s\".",
465 if (!home->port || (home->port > 65535)) {
466 cf_log_err(cf_sectiontoitem(cs),
467 "No port, or invalid port defined for home server %s.",
473 cf_log_err(cf_sectiontoitem(cs),
474 "Fatal error! Home server %s is ourselves!",
480 cf_log_err(cf_sectiontoitem(cs),
481 "No shared secret defined for home server %s.",
487 * Use a reasonable default.
490 if (!hs_type) hs_type = strdup("auth+acct");
492 if (strcasecmp(hs_type, "auth") == 0) {
493 home->type = HOME_TYPE_AUTH;
494 if (home->no_response_fail == 2) home->no_response_fail = 0;
496 } else if (strcasecmp(hs_type, "acct") == 0) {
497 home->type = HOME_TYPE_ACCT;
498 if (home->no_response_fail == 2) home->no_response_fail = 1;
500 } else if (strcasecmp(hs_type, "auth+acct") == 0) {
501 home->type = HOME_TYPE_AUTH;
505 } else if (strcasecmp(hs_type, "coa") == 0) {
506 home->type = HOME_TYPE_COA;
509 if (home->server != NULL) {
510 cf_log_err(cf_sectiontoitem(cs),
511 "Home servers of type \"coa\" cannot point to a virtual server");
517 cf_log_err(cf_sectiontoitem(cs),
518 "Invalid type \"%s\" for home server %s.",
525 if (!hs_check || (strcasecmp(hs_check, "none") == 0)) {
526 home->ping_check = HOME_PING_CHECK_NONE;
528 } else if (strcasecmp(hs_check, "status-server") == 0) {
529 home->ping_check = HOME_PING_CHECK_STATUS_SERVER;
531 } else if (strcasecmp(hs_check, "request") == 0) {
532 home->ping_check = HOME_PING_CHECK_REQUEST;
535 cf_log_err(cf_sectiontoitem(cs),
536 "Invalid ping_check \"%s\" for home server %s.",
543 if ((home->ping_check != HOME_PING_CHECK_NONE) &&
544 (home->ping_check != HOME_PING_CHECK_STATUS_SERVER)) {
545 if (!home->ping_user_name) {
546 cf_log_err(cf_sectiontoitem(cs), "You must supply a user name to enable ping checks");
550 if ((home->type == HOME_TYPE_AUTH) &&
551 !home->ping_user_password) {
552 cf_log_err(cf_sectiontoitem(cs), "You must supply a password to enable ping checks");
557 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
558 rbtree_finddata(home_servers_byaddr, home)) {
559 cf_log_err(cf_sectiontoitem(cs), "Duplicate home server");
564 * Look up the name using the *same* address family as
565 * for the home server.
567 if (hs_srcipaddr && (home->ipaddr.af != AF_UNSPEC)) {
568 if (ip_hton(hs_srcipaddr, home->ipaddr.af, &home->src_ipaddr) < 0) {
569 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
575 * Make sure that this is set.
577 if (home->src_ipaddr.af == AF_UNSPEC) {
578 home->src_ipaddr.af = home->ipaddr.af;
584 if (!rbtree_insert(home_servers_byname, home)) {
585 cf_log_err(cf_sectiontoitem(cs),
586 "Internal error %d adding home server %s.",
591 if ((home->ipaddr.af != AF_UNSPEC) && /* could be virtual server */
592 !rbtree_insert(home_servers_byaddr, home)) {
593 rbtree_deletebydata(home_servers_byname, home);
594 cf_log_err(cf_sectiontoitem(cs),
595 "Internal error %d adding home server %s.",
601 home->number = home_server_max_number++;
602 if (!rbtree_insert(home_servers_bynumber, home)) {
603 rbtree_deletebydata(home_servers_byname, home);
604 if (home->ipaddr.af != AF_UNSPEC) {
605 rbtree_deletebydata(home_servers_byname, home);
607 cf_log_err(cf_sectiontoitem(cs),
608 "Internal error %d adding home server %s.",
614 if (home->max_outstanding < 8) home->max_outstanding = 8;
615 if (home->max_outstanding > 65536*16) home->max_outstanding = 65536*16;
617 if (home->ping_interval < 6) home->ping_interval = 6;
618 if (home->ping_interval > 120) home->ping_interval = 120;
620 if (home->response_window < 1) home->response_window = 1;
621 if (home->response_window > 60) home->response_window = 60;
623 if (home->zombie_period < 1) home->zombie_period = 1;
624 if (home->zombie_period > 120) home->zombie_period = 120;
626 if (home->zombie_period < home->response_window) {
627 home->zombie_period = home->response_window;
630 if (home->num_pings_to_alive < 3) home->num_pings_to_alive = 3;
631 if (home->num_pings_to_alive > 10) home->num_pings_to_alive = 10;
633 if (home->ping_timeout < 3) home->ping_timeout = 3;
634 if (home->ping_timeout > 10) home->ping_timeout = 10;
636 if (home->revive_interval < 60) home->revive_interval = 60;
637 if (home->revive_interval > 3600) home->revive_interval = 3600;
640 if (home->coa_irt < 1) home->coa_irt = 1;
641 if (home->coa_irt > 5) home->coa_irt = 5;
643 if (home->coa_mrc < 0) home->coa_mrc = 0;
644 if (home->coa_mrc > 20 ) home->coa_mrc = 20;
646 if (home->coa_mrt < 0) home->coa_mrt = 0;
647 if (home->coa_mrt > 30 ) home->coa_mrt = 30;
649 if (home->coa_mrd < 5) home->coa_mrd = 5;
650 if (home->coa_mrd > 60 ) home->coa_mrd = 60;
654 home_server *home2 = rad_malloc(sizeof(*home2));
656 memcpy(home2, home, sizeof(*home2));
658 home2->type = HOME_TYPE_ACCT;
660 home2->ping_user_password = NULL;
663 if (home->no_response_fail == 2) home->no_response_fail = 0;
664 if (home2->no_response_fail == 2) home2->no_response_fail = 1;
666 if (!rbtree_insert(home_servers_byname, home2)) {
667 cf_log_err(cf_sectiontoitem(cs),
668 "Internal error %d adding home server %s.",
674 if ((home->ipaddr.af != AF_UNSPEC) &&
675 !rbtree_insert(home_servers_byaddr, home2)) {
676 rbtree_deletebydata(home_servers_byname, home2);
677 cf_log_err(cf_sectiontoitem(cs),
678 "Internal error %d adding home server %s.",
685 home2->number = home_server_max_number++;
686 if (!rbtree_insert(home_servers_bynumber, home2)) {
687 rbtree_deletebydata(home_servers_byname, home2);
688 if (home2->ipaddr.af != AF_UNSPEC) {
689 rbtree_deletebydata(home_servers_byname, home2);
691 cf_log_err(cf_sectiontoitem(cs),
692 "Internal error %d adding home server %s.",
701 * Mark it as already processed
703 cf_data_add(cs, "home_server", null_free, null_free);
709 static home_pool_t *server_pool_alloc(const char *name, home_pool_type_t type,
710 int server_type, int num_home_servers)
714 pool = rad_malloc(sizeof(*pool) + (sizeof(pool->servers[0]) *
716 if (!pool) return NULL; /* just for pairanoia */
718 memset(pool, 0, sizeof(*pool) + (sizeof(pool->servers[0]) *
723 pool->server_type = server_type;
724 pool->num_home_servers = num_home_servers;
729 static int pool_check_home_server(realm_config_t *rc, CONF_PAIR *cp,
730 const char *name, int server_type,
733 home_server myhome, *home;
734 CONF_SECTION *server_cs;
737 cf_log_err(cf_pairtoitem(cp),
738 "No value given for home_server.");
743 myhome.type = server_type;
744 home = rbtree_finddata(home_servers_byname, &myhome);
750 server_cs = cf_section_sub_find_name2(rc->cs, "home_server", name);
752 cf_log_err(cf_pairtoitem(cp),
753 "Unknown home_server \"%s\".", name);
757 home = rbtree_finddata(home_servers_byname, &myhome);
767 static int server_pool_add(realm_config_t *rc,
768 CONF_SECTION *cs, int server_type, int do_print)
771 home_pool_t *pool = NULL;
774 int num_home_servers;
777 name2 = cf_section_name1(cs);
778 if (!name2 || ((strcasecmp(name2, "server_pool") != 0) &&
779 (strcasecmp(name2, "home_server_pool") != 0))) {
780 cf_log_err(cf_sectiontoitem(cs),
781 "Section is not a home_server_pool.");
785 name2 = cf_section_name2(cs);
787 cf_log_err(cf_sectiontoitem(cs),
788 "Server pool section is missing a name.");
793 * Count the home servers and initalize them.
795 num_home_servers = 0;
796 for (cp = cf_pair_find(cs, "home_server");
798 cp = cf_pair_find_next(cs, cp, "home_server")) {
801 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
802 server_type, &home)) {
808 if (num_home_servers == 0) {
809 cf_log_err(cf_sectiontoitem(cs),
810 "No home servers defined in pool %s",
815 pool = server_pool_alloc(name2, HOME_POOL_FAIL_OVER, server_type,
821 * Fallback servers must be defined, and must be
824 cp = cf_pair_find(cs, "fallback");
827 if (server_type == HOME_TYPE_COA) {
828 cf_log_err(cf_sectiontoitem(cs), "Home server pools of type \"coa\" cannot have a fallback virtual server.");
833 if (!pool_check_home_server(rc, cp, cf_pair_value(cp),
834 server_type, &pool->fallback)) {
839 if (!pool->fallback->server) {
840 cf_log_err(cf_sectiontoitem(cs), "Fallback home_server %s does NOT contain a virtual_server directive.", pool->fallback->name);
845 if (do_print) cf_log_info(cs, " home_server_pool %s {", name2);
847 cp = cf_pair_find(cs, "type");
849 static FR_NAME_NUMBER pool_types[] = {
850 { "load-balance", HOME_POOL_LOAD_BALANCE },
851 { "fail-over", HOME_POOL_FAIL_OVER },
852 { "round_robin", HOME_POOL_LOAD_BALANCE },
853 { "fail_over", HOME_POOL_FAIL_OVER },
854 { "client-balance", HOME_POOL_CLIENT_BALANCE },
855 { "client-port-balance", HOME_POOL_CLIENT_PORT_BALANCE },
856 { "keyed-balance", HOME_POOL_KEYED_BALANCE },
860 value = cf_pair_value(cp);
862 cf_log_err(cf_pairtoitem(cp),
863 "No value given for type.");
867 pool->type = fr_str2int(pool_types, value, 0);
869 cf_log_err(cf_pairtoitem(cp),
870 "Unknown type \"%s\".",
875 if (do_print) cf_log_info(cs, "\ttype = %s", value);
878 cp = cf_pair_find(cs, "virtual_server");
880 pool->virtual_server = cf_pair_value(cp);
881 if (!pool->virtual_server) {
882 cf_log_err(cf_pairtoitem(cp), "No value given for virtual_server");
887 cf_log_info(cs, "\tvirtual_server = %s", pool->virtual_server);
890 if (!cf_section_sub_find_name2(rc->cs, "server",
891 pool->virtual_server)) {
892 cf_log_err(cf_pairtoitem(cp), "No such server %s",
893 pool->virtual_server);
899 num_home_servers = 0;
900 for (cp = cf_pair_find(cs, "home_server");
902 cp = cf_pair_find_next(cs, cp, "home_server")) {
905 value = cf_pair_value(cp);
907 memset(&myhome, 0, sizeof(&myhome));
909 myhome.type = server_type;
911 home = rbtree_finddata(home_servers_byname, &myhome);
913 DEBUG2("Internal sanity check failed");
918 DEBUG2("Warning: Duplicate home server %s in server pool %s", home->name, pool->name);
922 if (do_print) cf_log_info(cs, "\thome_server = %s", home->name);
923 pool->servers[num_home_servers++] = home;
924 } /* loop over home_server's */
926 if (pool->fallback && do_print) {
927 cf_log_info(cs, "\tfallback = %s", pool->fallback->name);
930 if (!rbtree_insert(home_pools_byname, pool)) {
931 rad_assert("Internal sanity check failed");
935 if (do_print) cf_log_info(cs, " }");
937 cf_data_add(cs, "home_server_pool", pool, free);
939 rad_assert(pool->server_type != 0);
944 if (do_print) cf_log_info(cs, " }");
950 static int old_server_add(realm_config_t *rc, CONF_SECTION *cs,
952 const char *name, const char *secret,
953 home_pool_type_t ldflag, home_pool_t **pool_p,
954 int type, const char *server)
957 int i, insert_point, num_home_servers;
958 home_server myhome, *home;
959 home_pool_t mypool, *pool;
962 rc = rc; /* -Wunused */
971 * LOCAL realms get sanity checked, and nothing else happens.
973 if (strcmp(name, "LOCAL") == 0) {
975 cf_log_err(cf_sectiontoitem(cs), "Realm \"%s\" cannot be both LOCAL and remote", name);
982 return 0; /* Not proxying. Can't do non-LOCAL realms */
986 mypool.server_type = type;
987 pool = rbtree_finddata(home_pools_byname, &mypool);
989 if (pool->type != ldflag) {
990 cf_log_err(cf_sectiontoitem(cs), "Inconsistent ldflag for server pool \"%s\"", name);
994 if (pool->server_type != type) {
995 cf_log_err(cf_sectiontoitem(cs), "Inconsistent home server type for server pool \"%s\"", name);
1002 home = rbtree_finddata(home_servers_byname, &myhome);
1004 if (secret && (strcmp(home->secret, secret) != 0)) {
1005 cf_log_err(cf_sectiontoitem(cs), "Inconsistent shared secret for home server \"%s\"", name);
1009 if (home->type != type) {
1010 cf_log_err(cf_sectiontoitem(cs), "Inconsistent type for home server \"%s\"", name);
1015 * See if the home server is already listed
1016 * in the pool. If so, do nothing else.
1018 if (pool) for (i = 0; i < pool->num_home_servers; i++) {
1019 if (pool->servers[i] == home) {
1026 * If we do have a pool, check that there is room to
1027 * insert the home server we've found, or the one that we
1030 * Note that we insert it into the LAST available
1031 * position, in order to maintain the same order as in
1032 * the configuration files.
1036 for (i = pool->num_home_servers - 1; i >= 0; i--) {
1037 if (pool->servers[i]) break;
1039 if (!pool->servers[i]) {
1044 if (insert_point < 0) {
1045 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);
1051 * No home server, allocate one.
1057 home = rad_malloc(sizeof(*home));
1058 memset(home, 0, sizeof(*home));
1061 home->hostname = name;
1063 home->secret = secret;
1066 p = strchr(name, ':');
1068 if (type == HOME_TYPE_AUTH) {
1069 home->port = PW_AUTH_UDP_PORT;
1071 home->port = PW_ACCT_UDP_PORT;
1077 } else if (p == name) {
1078 cf_log_err(cf_sectiontoitem(cs),
1079 "Invalid hostname %s.",
1085 home->port = atoi(p + 1);
1086 if ((home->port == 0) || (home->port > 65535)) {
1087 cf_log_err(cf_sectiontoitem(cs),
1094 q = rad_malloc((p - name) + 1);
1095 memcpy(q, name, (p - name));
1101 if (ip_hton(p, AF_UNSPEC, &home->ipaddr) < 0) {
1102 cf_log_err(cf_sectiontoitem(cs),
1103 "Failed looking up hostname %s.",
1110 home->ipaddr.af = AF_UNSPEC;
1111 home->server = server;
1116 * Use the old-style configuration.
1118 home->max_outstanding = 65535*16;
1119 home->zombie_period = rc->retry_delay * rc->retry_count;
1120 if (home->zombie_period == 0) home->zombie_period =30;
1121 home->response_window = home->zombie_period - 1;
1123 home->ping_check = HOME_PING_CHECK_NONE;
1125 home->revive_interval = rc->dead_time;
1127 if (rbtree_finddata(home_servers_byaddr, home)) {
1128 cf_log_err(cf_sectiontoitem(cs), "Home server %s has the same IP address and/or port as another home server.", name);
1133 if (!rbtree_insert(home_servers_byname, home)) {
1134 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1139 if (!rbtree_insert(home_servers_byaddr, home)) {
1140 rbtree_deletebydata(home_servers_byname, home);
1141 cf_log_err(cf_sectiontoitem(cs), "Internal error %d adding home server %s.", __LINE__, name);
1147 home->number = home_server_max_number++;
1148 if (!rbtree_insert(home_servers_bynumber, home)) {
1149 rbtree_deletebydata(home_servers_byname, home);
1150 if (home->ipaddr.af != AF_UNSPEC) {
1151 rbtree_deletebydata(home_servers_byname, home);
1153 cf_log_err(cf_sectiontoitem(cs),
1154 "Internal error %d adding home server %s.",
1163 * We now have a home server, see if we can insert it
1164 * into pre-existing pool.
1166 if (insert_point >= 0) {
1167 rad_assert(pool != NULL);
1168 pool->servers[insert_point] = home;
1172 rad_assert(pool == NULL);
1173 rad_assert(home != NULL);
1176 * Count the old-style realms of this name.
1178 num_home_servers = 0;
1179 for (subcs = cf_section_find_next(cs, NULL, "realm");
1181 subcs = cf_section_find_next(cs, subcs, "realm")) {
1182 const char *this = cf_section_name2(subcs);
1184 if (!this || (strcmp(this, realm) != 0)) continue;
1188 if (num_home_servers == 0) {
1189 cf_log_err(cf_sectiontoitem(cs), "Internal error counting pools for home server %s.", name);
1194 pool = server_pool_alloc(realm, ldflag, type, num_home_servers);
1197 pool->servers[0] = home;
1199 if (!rbtree_insert(home_pools_byname, pool)) {
1200 rad_assert("Internal sanity check failed");
1210 static int old_realm_config(realm_config_t *rc, CONF_SECTION *cs, REALM *r)
1213 const char *secret = NULL;
1214 home_pool_type_t ldflag;
1217 cp = cf_pair_find(cs, "ldflag");
1218 ldflag = HOME_POOL_FAIL_OVER;
1220 host = cf_pair_value(cp);
1222 cf_log_err(cf_pairtoitem(cp), "No value specified for ldflag");
1226 if (strcasecmp(host, "fail_over") == 0) {
1227 cf_log_info(cs, "\tldflag = fail_over");
1229 } else if (strcasecmp(host, "round_robin") == 0) {
1230 ldflag = HOME_POOL_LOAD_BALANCE;
1231 cf_log_info(cs, "\tldflag = round_robin");
1234 cf_log_err(cf_sectiontoitem(cs), "Unknown value \"%s\" for ldflag", host);
1237 } /* else don't print it. */
1240 * Allow old-style if it doesn't exist, or if it exists and
1243 cp = cf_pair_find(cs, "authhost");
1245 host = cf_pair_value(cp);
1247 cf_log_err(cf_pairtoitem(cp), "No value specified for authhost");
1251 if (strcmp(host, "LOCAL") != 0) {
1252 cp = cf_pair_find(cs, "secret");
1254 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1258 secret = cf_pair_value(cp);
1260 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1265 cf_log_info(cs, "\tauthhost = %s", host);
1267 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1268 &r->auth_pool, HOME_TYPE_AUTH, NULL)) {
1273 cp = cf_pair_find(cs, "accthost");
1275 host = cf_pair_value(cp);
1277 cf_log_err(cf_pairtoitem(cp), "No value specified for accthost");
1282 * Don't look for a secret again if it was found
1285 if ((strcmp(host, "LOCAL") != 0) && !secret) {
1286 cp = cf_pair_find(cs, "secret");
1288 cf_log_err(cf_sectiontoitem(cs), "No shared secret supplied for realm: %s", r->name);
1292 secret = cf_pair_value(cp);
1294 cf_log_err(cf_pairtoitem(cp), "No value specified for secret");
1299 cf_log_info(cs, "\taccthost = %s", host);
1301 if (!old_server_add(rc, cs, r->name, host, secret, ldflag,
1302 &r->acct_pool, HOME_TYPE_ACCT, NULL)) {
1307 cp = cf_pair_find(cs, "virtual_server");
1309 host = cf_pair_value(cp);
1311 cf_log_err(cf_pairtoitem(cp), "No value specified for virtual_server");
1315 cf_log_info(cs, "\tvirtual_server = %s", host);
1317 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1318 &r->auth_pool, HOME_TYPE_AUTH, host)) {
1321 if (!old_server_add(rc, cs, r->name, host, "", ldflag,
1322 &r->acct_pool, HOME_TYPE_ACCT, host)) {
1327 if (secret) cf_log_info(cs, "\tsecret = %s", secret);
1335 static int add_pool_to_realm(realm_config_t *rc, CONF_SECTION *cs,
1336 const char *name, home_pool_t **dest,
1337 int server_type, int do_print)
1339 home_pool_t mypool, *pool;
1342 mypool.server_type = server_type;
1344 pool = rbtree_finddata(home_pools_byname, &mypool);
1346 CONF_SECTION *pool_cs;
1348 pool_cs = cf_section_sub_find_name2(rc->cs,
1352 pool_cs = cf_section_sub_find_name2(rc->cs,
1357 cf_log_err(cf_sectiontoitem(cs), "Failed to find home_server_pool \"%s\"", name);
1361 if (!server_pool_add(rc, pool_cs, server_type, do_print)) {
1365 pool = rbtree_finddata(home_pools_byname, &mypool);
1367 radlog(L_ERR, "Internal sanity check failed in add_pool_to_realm");
1372 if (pool->server_type != server_type) {
1373 cf_log_err(cf_sectiontoitem(cs), "Incompatible home_server_pool \"%s\" (mixed auth_pool / acct_pool)", name);
1384 static int realm_add(realm_config_t *rc, CONF_SECTION *cs)
1390 home_pool_t *auth_pool, *acct_pool;
1391 const char *auth_pool_name, *acct_pool_name;
1394 name2 = cf_section_name1(cs);
1395 if (!name2 || (strcasecmp(name2, "realm") != 0)) {
1396 cf_log_err(cf_sectiontoitem(cs), "Section is not a realm.");
1400 name2 = cf_section_name2(cs);
1402 cf_log_err(cf_sectiontoitem(cs), "Realm section is missing the realm name.");
1407 auth_pool = acct_pool = NULL;
1408 auth_pool_name = acct_pool_name = NULL;
1411 * Prefer new configuration to old one.
1413 cp = cf_pair_find(cs, "pool");
1414 if (!cp) cp = cf_pair_find(cs, "home_server_pool");
1415 if (cp) auth_pool_name = cf_pair_value(cp);
1416 if (cp && auth_pool_name) {
1417 acct_pool_name = auth_pool_name;
1418 if (!add_pool_to_realm(rc, cs,
1419 auth_pool_name, &auth_pool,
1420 HOME_TYPE_AUTH, 1)) {
1423 if (!add_pool_to_realm(rc, cs,
1424 auth_pool_name, &acct_pool,
1425 HOME_TYPE_ACCT, 0)) {
1430 cp = cf_pair_find(cs, "auth_pool");
1431 if (cp) auth_pool_name = cf_pair_value(cp);
1432 if (cp && auth_pool_name) {
1434 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"auth_pool\" at the same time.");
1437 if (!add_pool_to_realm(rc, cs,
1438 auth_pool_name, &auth_pool,
1439 HOME_TYPE_AUTH, 1)) {
1444 cp = cf_pair_find(cs, "acct_pool");
1445 if (cp) acct_pool_name = cf_pair_value(cp);
1446 if (cp && acct_pool_name) {
1447 int do_print = TRUE;
1450 cf_log_err(cf_sectiontoitem(cs), "Cannot use \"pool\" and \"acct_pool\" at the same time.");
1455 (strcmp(auth_pool_name, acct_pool_name) != 0)) {
1459 if (!add_pool_to_realm(rc, cs,
1460 acct_pool_name, &acct_pool,
1461 HOME_TYPE_ACCT, do_print)) {
1467 cf_log_info(cs, " realm %s {", name2);
1471 * The realm MAY already exist if it's an old-style realm.
1472 * In that case, merge the old-style realm with this one.
1474 r = realm_find2(name2);
1475 if (r && (strcmp(r->name, name2) == 0)) {
1476 if (cf_pair_find(cs, "auth_pool") ||
1477 cf_pair_find(cs, "acct_pool")) {
1478 cf_log_err(cf_sectiontoitem(cs), "Duplicate realm \"%s\"", name2);
1482 if (!old_realm_config(rc, cs, r)) {
1486 cf_log_info(cs, " } # realm %s", name2);
1492 if (name2[0] == '~') {
1497 * Include substring matches.
1499 rcode = regcomp(®, name2 + 1,
1500 REG_EXTENDED | REG_NOSUB | REG_ICASE);
1504 regerror(rcode, ®, buffer, sizeof(buffer));
1506 cf_log_err(cf_sectiontoitem(cs),
1507 "Invalid regex \"%s\": %s",
1515 r = rad_malloc(sizeof(*r));
1516 memset(r, 0, sizeof(*r));
1521 r->auth_pool = auth_pool;
1522 r->acct_pool = acct_pool;
1524 if (auth_pool_name &&
1525 (auth_pool_name == acct_pool_name)) { /* yes, ptr comparison */
1526 cf_log_info(cs, "\tpool = %s", auth_pool_name);
1528 if (auth_pool_name) cf_log_info(cs, "\tauth_pool = %s", auth_pool_name);
1529 if (acct_pool_name) cf_log_info(cs, "\tacct_pool = %s", acct_pool_name);
1533 cp = cf_pair_find(cs, "nostrip");
1534 if (cp && (cf_pair_value(cp) == NULL)) {
1536 cf_log_info(cs, "\tnostrip");
1540 * We're a new-style realm. Complain if we see the old
1543 if (r->auth_pool || r->acct_pool) {
1544 if (((cp = cf_pair_find(cs, "authhost")) != NULL) ||
1545 ((cp = cf_pair_find(cs, "accthost")) != NULL) ||
1546 ((cp = cf_pair_find(cs, "secret")) != NULL) ||
1547 ((cp = cf_pair_find(cs, "ldflag")) != NULL)) {
1548 DEBUG2("WARNING: Ignoring old-style configuration entry \"%s\" in realm \"%s\"", cf_pair_attr(cp), r->name);
1553 * The realm MAY be an old-style realm, as there
1554 * was no auth_pool or acct_pool. Double-check
1555 * it, just to be safe.
1557 } else if (!old_realm_config(rc, cs, r)) {
1563 * It's a regex. Add it to a separate list.
1565 if (name2[0] == '~') {
1566 realm_regex_t *rr, **last;
1568 rr = rad_malloc(sizeof(*rr));
1570 last = &realms_regex;
1571 while (*last) last = &((*last)->next); /* O(N^2)... sue me. */
1579 cf_log_info(cs, " }");
1584 if (!rbtree_insert(realms_byname, r)) {
1585 rad_assert("Internal sanity check failed");
1589 cf_log_info(cs, " }");
1594 cf_log_info(cs, " } # realm %s", name2);
1600 static const FR_NAME_NUMBER home_server_types[] = {
1601 { "auth", HOME_TYPE_AUTH },
1602 { "auth+acct", HOME_TYPE_AUTH },
1603 { "acct", HOME_TYPE_ACCT },
1604 { "coa", HOME_TYPE_COA },
1608 static int pool_peek_type(CONF_SECTION *config, CONF_SECTION *cs)
1611 const char *name, *type;
1613 CONF_SECTION *server_cs;
1615 cp = cf_pair_find(cs, "home_server");
1617 cf_log_err(cf_sectiontoitem(cs), "Pool does not contain a \"home_server\" entry");
1618 return HOME_TYPE_INVALID;
1621 name = cf_pair_value(cp);
1623 cf_log_err(cf_pairtoitem(cp), "home_server entry does not reference a home server");
1624 return HOME_TYPE_INVALID;
1627 server_cs = cf_section_sub_find_name2(config, "home_server", name);
1629 cf_log_err(cf_pairtoitem(cp), "home_server \"%s\" does not exist", name);
1630 return HOME_TYPE_INVALID;
1633 cp = cf_pair_find(server_cs, "type");
1635 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s does not contain a \"type\" entry", name);
1636 return HOME_TYPE_INVALID;
1639 type = cf_pair_value(cp);
1641 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an empty \"type\" entry", name);
1642 return HOME_TYPE_INVALID;
1645 home = fr_str2int(home_server_types, type, HOME_TYPE_INVALID);
1646 if (home == HOME_TYPE_INVALID) {
1647 cf_log_err(cf_sectiontoitem(server_cs), "home_server %s contains an invalid \"type\" entry of value \"%s\"", name, type);
1648 return HOME_TYPE_INVALID;
1651 return home; /* 'cause we miss it so much */
1655 int realms_init(CONF_SECTION *config)
1658 realm_config_t *rc, *old_rc;
1660 if (realms_byname) return 1;
1662 realms_byname = rbtree_create(realm_name_cmp, free, 0);
1663 if (!realms_byname) {
1669 home_servers_byaddr = rbtree_create(home_server_addr_cmp, free, 0);
1670 if (!home_servers_byaddr) {
1675 home_servers_byname = rbtree_create(home_server_name_cmp, NULL, 0);
1676 if (!home_servers_byname) {
1682 home_servers_bynumber = rbtree_create(home_server_number_cmp, NULL, 0);
1683 if (!home_servers_bynumber) {
1689 home_pools_byname = rbtree_create(home_pool_name_cmp, NULL, 0);
1690 if (!home_pools_byname) {
1696 rc = rad_malloc(sizeof(*rc));
1697 memset(rc, 0, sizeof(*rc));
1701 cs = cf_subsection_find_next(config, NULL, "proxy");
1703 cf_section_parse(cs, rc, proxy_config);
1705 rc->dead_time = DEAD_TIME;
1706 rc->retry_count = RETRY_COUNT;
1707 rc->retry_delay = RETRY_DELAY;
1709 rc->wake_all_if_all_dead= 0;
1713 for (cs = cf_subsection_find_next(config, NULL, "home_server");
1715 cs = cf_subsection_find_next(config, cs, "home_server")) {
1716 if (!home_server_add(rc, cs)) {
1723 for (cs = cf_subsection_find_next(config, NULL, "realm");
1725 cs = cf_subsection_find_next(config, cs, "realm")) {
1726 if (!realm_add(rc, cs)) {
1735 * CoA pools aren't tied to realms.
1737 for (cs = cf_subsection_find_next(config, NULL, "home_server_pool");
1739 cs = cf_subsection_find_next(config, cs, "home_server_pool")) {
1743 * Pool was already loaded.
1745 if (cf_data_find(cs, "home_server_pool")) continue;
1747 type = pool_peek_type(config, cs);
1748 if (type == HOME_TYPE_INVALID) return 0;
1750 if (!server_pool_add(rc, cs, type, TRUE)) {
1758 xlat_register("home_server", xlat_home_server, NULL);
1759 xlat_register("home_server_pool", xlat_server_pool, NULL);
1763 * Swap pointers atomically.
1765 old_rc = realm_config;
1773 * Find a realm where "name" might be the regex.
1775 REALM *realm_find2(const char *name)
1780 if (!name) name = "NULL";
1782 myrealm.name = name;
1783 realm = rbtree_finddata(realms_byname, &myrealm);
1784 if (realm) return realm;
1788 realm_regex_t *this;
1790 for (this = realms_regex; this != NULL; this = this->next) {
1791 if (strcmp(this->realm->name, name) == 0) {
1799 * Couldn't find a realm. Look for DEFAULT.
1801 myrealm.name = "DEFAULT";
1802 return rbtree_finddata(realms_byname, &myrealm);
1807 * Find a realm in the REALM list.
1809 REALM *realm_find(const char *name)
1814 if (!name) name = "NULL";
1816 myrealm.name = name;
1817 realm = rbtree_finddata(realms_byname, &myrealm);
1818 if (realm) return realm;
1822 realm_regex_t *this;
1824 for (this = realms_regex; this != NULL; this = this->next) {
1829 * Include substring matches.
1831 if (regcomp(®, this->realm->name + 1,
1832 REG_EXTENDED | REG_NOSUB | REG_ICASE) != 0) {
1836 compare = regexec(®, name, 0, NULL, 0);
1839 if (compare == 0) return this->realm;
1845 * Couldn't find a realm. Look for DEFAULT.
1847 myrealm.name = "DEFAULT";
1848 return rbtree_finddata(realms_byname, &myrealm);
1853 home_server *home_server_ldb(const char *realmname,
1854 home_pool_t *pool, REQUEST *request)
1858 home_server *found = NULL;
1859 home_server *zombie = NULL;
1863 * Determine how to pick choose the home server.
1865 switch (pool->type) {
1869 * For load-balancing by client IP address, we
1870 * pick a home server by hashing the client IP.
1872 * This isn't as even a load distribution as
1873 * tracking the State attribute, but it's better
1876 case HOME_POOL_CLIENT_BALANCE:
1877 switch (request->packet->src_ipaddr.af) {
1879 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1880 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1883 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1884 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1890 start = hash % pool->num_home_servers;
1893 case HOME_POOL_CLIENT_PORT_BALANCE:
1894 switch (request->packet->src_ipaddr.af) {
1896 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip4addr,
1897 sizeof(request->packet->src_ipaddr.ipaddr.ip4addr));
1900 hash = fr_hash(&request->packet->src_ipaddr.ipaddr.ip6addr,
1901 sizeof(request->packet->src_ipaddr.ipaddr.ip6addr));
1907 fr_hash_update(&request->packet->src_port,
1908 sizeof(request->packet->src_port), hash);
1909 start = hash % pool->num_home_servers;
1912 case HOME_POOL_KEYED_BALANCE:
1913 if ((vp = pairfind(request->config_items, PW_LOAD_BALANCE_KEY)) != NULL) {
1914 hash = fr_hash(vp->vp_strvalue, vp->length);
1915 start = hash % pool->num_home_servers;
1920 case HOME_POOL_LOAD_BALANCE:
1921 case HOME_POOL_FAIL_OVER:
1925 default: /* this shouldn't happen... */
1932 * Starting with the home server we chose, loop through
1933 * all home servers. If the current one is dead, skip
1934 * it. If it is too busy, skip it.
1936 * Otherwise, use it.
1938 for (count = 0; count < pool->num_home_servers; count++) {
1939 home_server *home = pool->servers[(start + count) % pool->num_home_servers];
1941 if (!home) continue;
1944 * Skip dead home servers.
1946 if (home->state == HOME_STATE_IS_DEAD) {
1951 * This home server is too busy. Choose another one.
1953 if (home->currently_outstanding >= home->max_outstanding) {
1959 * We read the packet from a detail file, AND it
1960 * came from this server. Don't re-proxy it
1963 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
1964 (request->packet->code == PW_ACCOUNTING_REQUEST) &&
1965 (fr_ipaddr_cmp(&home->ipaddr, &request->packet->src_ipaddr) == 0)) {
1971 * It's zombie, so we remember the first zombie
1972 * we find, but we don't mark it as a "live"
1975 if (home->state == HOME_STATE_ZOMBIE) {
1976 if (!zombie) zombie = home;
1981 * We've found the first "live" one. Use that.
1983 if (pool->type != HOME_POOL_LOAD_BALANCE) {
1989 * Otherwise we're doing some kind of load balancing.
1990 * If we haven't found one yet, pick this one.
1997 RDEBUG3("PROXY %s %d\t%s %d",
1998 found->name, found->currently_outstanding,
1999 home->name, home->currently_outstanding);
2002 * Prefer this server if it's less busy than the
2003 * one we had previously found.
2005 if (home->currently_outstanding < found->currently_outstanding) {
2006 RDEBUG3("PROXY Choosing %s: It's less busy than %s",
2007 home->name, found->name);
2013 * Ignore servers which are busier than the one
2016 if (home->currently_outstanding > found->currently_outstanding) {
2017 RDEBUG3("PROXY Skipping %s: It's busier than %s",
2018 home->name, found->name);
2023 * From the list of servers which have the same
2024 * load, choose one at random.
2026 if (((count + 1) * (fr_rand() & 0xffff)) < (uint32_t) 0x10000) {
2029 } /* loop over the home servers */
2032 * We have no live servers, BUT we have a zombie. Use
2033 * the zombie as a last resort.
2035 if (!found && zombie) {
2041 * There's a fallback if they're all dead.
2043 if (!found && pool->fallback) {
2044 found = pool->fallback;
2050 * Allocate the proxy packet, only if it wasn't
2051 * already allocated by a module. This check is
2052 * mainly to support the proxying of EAP-TTLS and
2053 * EAP-PEAP tunneled requests.
2055 * In those cases, the EAP module creates a
2056 * "fake" request, and recursively passes it
2057 * through the authentication stage of the
2058 * server. The module then checks if the request
2059 * was supposed to be proxied, and if so, creates
2060 * a proxy packet from the TUNNELED request, and
2061 * not from the EAP request outside of the
2064 * The proxy then works like normal, except that
2065 * the response packet is "eaten" by the EAP
2066 * module, and encapsulated into an EAP packet.
2068 if (!request->proxy) {
2069 if ((request->proxy = rad_alloc(TRUE)) == NULL) {
2070 radlog(L_ERR|L_CONS, "no memory");
2075 * Copy the request, then look up name
2076 * and plain-text password in the copy.
2078 * Note that the User-Name attribute is
2079 * the *original* as sent over by the
2080 * client. The Stripped-User-Name
2081 * attribute is the one hacked through
2084 request->proxy->vps = paircopy(request->packet->vps);
2088 * Update the various fields as appropriate.
2090 request->proxy->src_ipaddr = found->src_ipaddr;
2091 request->proxy->dst_ipaddr = found->ipaddr;
2092 request->proxy->dst_port = found->port;
2093 request->home_server = found;
2096 * We're supposed to add a Message-Authenticator
2097 * if it doesn't exist, and it doesn't exist.
2099 if (found->message_authenticator &&
2100 (request->packet->code == PW_AUTHENTICATION_REQUEST) &&
2101 !pairfind(request->proxy->vps, PW_MESSAGE_AUTHENTICATOR)) {
2102 radius_pairmake(request, &request->proxy->vps,
2103 "Message-Authenticator", "0x00",
2111 * No live match found, and no fallback to the "DEFAULT"
2112 * realm. We fix this by blindly marking all servers as
2113 * "live". But only do it for ones that don't support
2114 * "pings", as they will be marked live when they
2115 * actually are live.
2117 if (!realm_config->fallback &&
2118 realm_config->wake_all_if_all_dead) {
2119 for (count = 0; count < pool->num_home_servers; count++) {
2120 home_server *home = pool->servers[count];
2122 if (!home) continue;
2124 if ((home->state == HOME_STATE_IS_DEAD) &&
2125 (home->ping_check == HOME_PING_CHECK_NONE)) {
2126 home->state = HOME_STATE_ALIVE;
2127 if (!found) found = home;
2131 if (found) goto update_and_return;
2135 * Still nothing. Look up the DEFAULT realm, but only
2136 * if we weren't looking up the NULL or DEFAULT realms.
2138 if (realm_config->fallback &&
2140 (strcmp(realmname, "NULL") != 0) &&
2141 (strcmp(realmname, "DEFAULT") != 0)) {
2142 REALM *rd = realm_find("DEFAULT");
2144 if (!rd) return NULL;
2147 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
2148 pool = rd->auth_pool;
2150 } else if (request->packet->code == PW_ACCOUNTING_REQUEST) {
2151 pool = rd->acct_pool;
2153 if (!pool) return NULL;
2155 RDEBUG2("PROXY - realm %s has no live home servers. Falling back to the DEFAULT realm.", realmname);
2156 return home_server_ldb(rd->name, pool, request);
2160 * Still haven't found anything. Oh well.
2166 home_server *home_server_find(fr_ipaddr_t *ipaddr, int port)
2170 memset(&myhome, 0, sizeof(myhome));
2171 myhome.ipaddr = *ipaddr;
2173 myhome.server = NULL; /* we're not called for internal proxying */
2175 return rbtree_finddata(home_servers_byaddr, &myhome);
2179 home_server *home_server_byname(const char *name, int type)
2183 memset(&myhome, 0, sizeof(myhome));
2187 return rbtree_finddata(home_servers_byname, &myhome);
2192 home_server *home_server_bynumber(int number)
2196 memset(&myhome, 0, sizeof(myhome));
2197 myhome.number = number;
2198 myhome.server = NULL; /* we're not called for internal proxying */
2200 return rbtree_finddata(home_servers_bynumber, &myhome);
2204 home_pool_t *home_pool_byname(const char *name, int type)
2208 memset(&mypool, 0, sizeof(mypool));
2210 mypool.server_type = type;
2211 return rbtree_finddata(home_pools_byname, &mypool);
2217 static int home_server_create_callback(UNUSED void *ctx, void *data)
2219 home_server *home = data;
2223 * If there WAS a src address defined, ensure that a
2224 * proxy listener has been defined.
2226 if (home->src_ipaddr.af != AF_UNSPEC) {
2227 this = proxy_new_listener(&home->src_ipaddr, TRUE);
2230 * Failed to create it: Die
2232 if (!this) return 1;
2235 * Don't do anything else. The function above
2236 * takes care of adding the listener to the list.
2244 * Taking a void* here solves some header issues.
2246 int home_server_create_listeners(void)
2248 if (!home_servers_byaddr) return 0;
2251 * Add the listeners to the TAIL of the list.
2253 if (rbtree_walk(home_servers_byaddr, InOrder,
2254 home_server_create_callback, NULL) != 0) {