2 * client.c Read clients into memory.
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 2000,2006 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/rad_assert.h>
36 #ifdef WITH_DYNAMIC_CLIENTS
42 struct radclient_list {
44 * FIXME: One set of trees for IPv4, and another for IPv6?
46 rbtree_t *trees[129]; /* for 0..128, inclusive. */
52 static rbtree_t *tree_num = NULL; /* client numbers 0..N */
53 static int tree_num_max = 0;
55 static RADCLIENT_LIST *root_clients = NULL;
57 #ifdef WITH_DYNAMIC_CLIENTS
58 static fr_fifo_t *deleted_clients = NULL;
62 * Callback for freeing a client.
64 void client_free(RADCLIENT *client)
68 #ifdef WITH_DYNAMIC_CLIENTS
69 if (client->dynamic == 2) {
72 if (!deleted_clients) {
73 deleted_clients = fr_fifo_create(1024,
74 (void *) client_free);
75 if (!deleted_clients) return; /* MEMLEAK */
79 * Mark it as in the fifo, and remember when we
83 client->created = now = time(NULL); /* re-set it */
84 fr_fifo_push(deleted_clients, client);
87 * Peek at the head of the fifo. If it might
88 * still be in use, return. Otherwise, pop it
89 * from the queue and delete it.
91 client = fr_fifo_peek(deleted_clients);
92 if ((client->created + 120) >= now) return;
94 client = fr_fifo_pop(deleted_clients);
95 rad_assert(client != NULL);
103 * Callback for comparing two clients.
105 static int client_ipaddr_cmp(const void *one, const void *two)
107 const RADCLIENT *a = one;
108 const RADCLIENT *b = two;
111 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
115 rcode = fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
116 if (rcode != 0) return rcode;
121 if ((a->proto == IPPROTO_IP) ||
122 (b->proto == IPPROTO_IP)) return 0;
124 return (a->proto - b->proto);
129 static int client_num_cmp(const void *one, const void *two)
131 const RADCLIENT *a = one;
132 const RADCLIENT *b = two;
134 return (a->number - b->number);
139 * Free a RADCLIENT list.
141 void clients_free(RADCLIENT_LIST *clients)
145 if (!clients) return;
147 for (i = 0; i <= 128; i++) {
148 if (clients->trees[i]) rbtree_free(clients->trees[i]);
149 clients->trees[i] = NULL;
152 if (clients == root_clients) {
154 if (tree_num) rbtree_free(tree_num);
161 #ifdef WITH_DYNAMIC_CLIENTS
163 * FIXME: No fr_fifo_delete()
167 talloc_free(clients);
171 * Return a new, initialized, set of clients.
173 RADCLIENT_LIST *clients_init(void)
175 RADCLIENT_LIST *clients = talloc_zero(NULL, RADCLIENT_LIST);
177 if (!clients) return NULL;
179 clients->min_prefix = 128;
186 * Sanity check a client.
188 static int client_sane(RADCLIENT *client)
190 switch (client->ipaddr.af) {
192 if (client->prefix > 32) {
197 * Zero out the subnet bits.
199 if (client->prefix == 0) {
200 memset(&client->ipaddr.ipaddr.ip4addr, 0,
201 sizeof(client->ipaddr.ipaddr.ip4addr));
203 } else if (client->prefix < 32) {
206 mask <<= (32 - client->prefix);
207 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
212 if (client->prefix > 128) return 0;
214 if (client->prefix == 0) {
215 memset(&client->ipaddr.ipaddr.ip6addr, 0,
216 sizeof(client->ipaddr.ipaddr.ip6addr));
218 } else if (client->prefix < 128) {
219 uint32_t mask, *addr;
221 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
223 if ((client->prefix & 0x1f) == 0) {
226 mask = ~ ((uint32_t) 0);
227 mask <<= (32 - (client->prefix & 0x1f));
231 switch (client->prefix >> 5) {
260 * Add a client to the tree.
262 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
271 * If "clients" is NULL, it means add to the global list.
275 * Initialize it, if not done already.
278 root_clients = clients_init();
279 if (!root_clients) return 0;
281 clients = root_clients;
284 if ((client->prefix < 0) || (client->prefix > 128)) {
288 if (!client_sane(client)) return 0;
291 * Create a tree for it.
293 if (!clients->trees[client->prefix]) {
294 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
295 (void *) client_free, 0);
296 if (!clients->trees[client->prefix]) {
301 #define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0)))
304 * Cannot insert the same client twice.
306 old = rbtree_finddata(clients->trees[client->prefix], client);
309 * If it's a complete duplicate, then free the new
310 * one, and return "OK".
312 if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) &&
313 (old->prefix == client->prefix) &&
314 namecmp(longname) && namecmp(secret) &&
315 namecmp(shortname) && namecmp(nastype) &&
316 namecmp(login) && namecmp(password) && namecmp(server) &&
317 #ifdef WITH_DYNAMIC_CLIENTS
318 (old->lifetime == client->lifetime) &&
319 namecmp(client_server) &&
323 (old->coa_server == client->coa_server) &&
324 (old->coa_pool == client->coa_pool) &&
326 (old->message_authenticator == client->message_authenticator)) {
327 DEBUGW("Ignoring duplicate client %s", client->longname);
332 radlog(L_ERR, "Failed to add duplicate client %s",
339 * Other error adding client: likely is fatal.
341 if (!rbtree_insert(clients->trees[client->prefix], client)) {
347 tree_num = rbtree_create(client_num_cmp, NULL, 0);
350 #ifdef WITH_DYNAMIC_CLIENTS
352 * More catching of clients added by rlm_sql.
354 * The sql modules sets the dynamic flag BEFORE calling
355 * us. The client_create() function sets it AFTER
358 if (client->dynamic && (client->lifetime == 0)) {
362 * If there IS an enclosing network,
363 * inherit the lifetime from it.
365 network = client_find(clients, &client->ipaddr, client->proto);
367 client->lifetime = network->lifetime;
372 client->number = tree_num_max;
374 if (tree_num) rbtree_insert(tree_num, client);
377 if (client->prefix < clients->min_prefix) {
378 clients->min_prefix = client->prefix;
385 #ifdef WITH_DYNAMIC_CLIENTS
386 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
390 if (!clients) clients = root_clients;
392 if (!client->dynamic) return;
394 rad_assert((client->prefix >= 0) && (client->prefix <= 128));
396 client->dynamic = 2; /* signal to client_free */
399 rbtree_deletebydata(tree_num, client);
401 rbtree_deletebydata(clients->trees[client->prefix], client);
407 * Find a client in the RADCLIENTS list by number.
408 * This is a support function for the statistics code.
410 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
414 if (!clients) clients = root_clients;
416 if (!clients) return NULL;
418 if (number >= tree_num_max) return NULL;
423 myclient.number = number;
425 return rbtree_finddata(tree_num, &myclient);
428 clients = clients; /* -Wunused */
429 number = number; /* -Wunused */
436 * Find a client in the RADCLIENTS list.
438 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
439 const fr_ipaddr_t *ipaddr, int proto)
444 if (!clients) clients = root_clients;
446 if (!clients || !ipaddr) return NULL;
448 switch (ipaddr->af) {
461 for (i = max_prefix; i >= clients->min_prefix; i--) {
465 myclient.ipaddr = *ipaddr;
466 myclient.proto = proto;
467 client_sane(&myclient); /* clean up the ipaddress */
469 if (!clients->trees[i]) continue;
471 data = rbtree_finddata(clients->trees[i], &myclient);
482 * Old wrapper for client_find
484 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
486 return client_find(root_clients, ipaddr, IPPROTO_UDP);
489 static struct in_addr cl_ip4addr;
490 static struct in6_addr cl_ip6addr;
491 static char *cl_srcipaddr = NULL;
493 static char *hs_proto = NULL;
497 static CONF_PARSER limit_config[] = {
498 { "max_connections", PW_TYPE_INTEGER,
499 offsetof(RADCLIENT, limit.max_connections), NULL, "16" },
501 { "lifetime", PW_TYPE_INTEGER,
502 offsetof(RADCLIENT, limit.lifetime), NULL, "0" },
504 { "idle_timeout", PW_TYPE_INTEGER,
505 offsetof(RADCLIENT, limit.idle_timeout), NULL, "30" },
507 { NULL, -1, 0, NULL, NULL } /* end the list */
511 static const CONF_PARSER client_config[] = {
512 { "ipaddr", PW_TYPE_IPADDR,
513 0, &cl_ip4addr, NULL },
514 { "ipv6addr", PW_TYPE_IPV6ADDR,
515 0, &cl_ip6addr, NULL },
516 { "netmask", PW_TYPE_INTEGER,
517 offsetof(RADCLIENT, prefix), 0, NULL },
519 { "src_ipaddr", PW_TYPE_STRING_PTR,
520 0, &cl_srcipaddr, NULL },
522 { "require_message_authenticator", PW_TYPE_BOOLEAN,
523 offsetof(RADCLIENT, message_authenticator), 0, "no" },
525 { "secret", PW_TYPE_STRING_PTR,
526 offsetof(RADCLIENT, secret), 0, NULL },
527 { "shortname", PW_TYPE_STRING_PTR,
528 offsetof(RADCLIENT, shortname), 0, NULL },
529 { "nastype", PW_TYPE_STRING_PTR,
530 offsetof(RADCLIENT, nastype), 0, NULL },
531 { "login", PW_TYPE_STRING_PTR,
532 offsetof(RADCLIENT, login), 0, NULL },
533 { "password", PW_TYPE_STRING_PTR,
534 offsetof(RADCLIENT, password), 0, NULL },
535 { "virtual_server", PW_TYPE_STRING_PTR,
536 offsetof(RADCLIENT, server), 0, NULL },
537 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
538 offsetof(RADCLIENT, server), 0, NULL },
541 { "proto", PW_TYPE_STRING_PTR,
542 0, &hs_proto, NULL },
544 { "limit", PW_TYPE_SUBSECTION, 0, NULL, (const void *) limit_config },
547 #ifdef WITH_DYNAMIC_CLIENTS
548 { "dynamic_clients", PW_TYPE_STRING_PTR,
549 offsetof(RADCLIENT, client_server), 0, NULL },
550 { "lifetime", PW_TYPE_INTEGER,
551 offsetof(RADCLIENT, lifetime), 0, NULL },
552 { "rate_limit", PW_TYPE_BOOLEAN,
553 offsetof(RADCLIENT, rate_limit), 0, NULL },
557 { "coa_server", PW_TYPE_STRING_PTR,
558 offsetof(RADCLIENT, coa_name), 0, NULL },
561 { NULL, -1, 0, NULL, NULL }
565 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
570 name2 = cf_section_name2(cs);
572 cf_log_err(cf_sectiontoitem(cs),
573 "Missing client name");
578 * The size is fine.. Let's create the buffer
580 c = talloc_zero(cs, RADCLIENT);
583 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
584 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
587 if (cf_section_parse(cs, c, client_config) < 0) {
588 cf_log_err(cf_sectiontoitem(cs),
589 "Error parsing client section.");
601 * Global clients can set servers to use,
602 * per-server clients cannot.
604 if (in_server && c->server) {
605 cf_log_err(cf_sectiontoitem(cs),
606 "Clients inside of an server section cannot point to a server.");
611 * No "ipaddr" or "ipv6addr", use old-style
612 * "client <ipaddr> {" syntax.
614 if (!cf_pair_find(cs, "ipaddr") &&
615 !cf_pair_find(cs, "ipv6addr")) {
618 prefix_ptr = strchr(name2, '/');
624 c->prefix = atoi(prefix_ptr + 1);
625 if ((c->prefix < 0) || (c->prefix > 128)) {
626 cf_log_err(cf_sectiontoitem(cs),
627 "Invalid Prefix value '%s' for IP.",
631 /* Replace '/' with '\0' */
636 * Always get the numeric representation of IP
638 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
639 cf_log_err(cf_sectiontoitem(cs),
640 "Failed to look up hostname %s: %s",
641 name2, fr_strerror());
645 if (prefix_ptr) *prefix_ptr = '/';
646 c->longname = strdup(name2);
648 if (!c->shortname) c->shortname = strdup(c->longname);
654 * Figure out which one to use.
656 if (cf_pair_find(cs, "ipaddr")) {
657 c->ipaddr.af = AF_INET;
658 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
660 if ((c->prefix < -1) || (c->prefix > 32)) {
661 cf_log_err(cf_sectiontoitem(cs),
662 "Netmask must be between 0 and 32");
666 } else if (cf_pair_find(cs, "ipv6addr")) {
667 c->ipaddr.af = AF_INET6;
668 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
670 if ((c->prefix < -1) || (c->prefix > 128)) {
671 cf_log_err(cf_sectiontoitem(cs),
672 "Netmask must be between 0 and 128");
676 cf_log_err(cf_sectiontoitem(cs),
677 "No IP address defined for the client");
681 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
682 c->longname = strdup(buffer);
685 * Set the short name to the name2
687 if (!c->shortname) c->shortname = strdup(name2);
689 c->proto = IPPROTO_UDP;
692 if (strcmp(hs_proto, "udp") == 0) {
695 } else if (strcmp(hs_proto, "tcp") == 0) {
697 c->proto = IPPROTO_TCP;
699 } else if (strcmp(hs_proto, "*") == 0) {
701 c->proto = IPPROTO_IP; /* fake for dual */
704 cf_log_err(cf_sectiontoitem(cs),
705 "Unknown proto \"%s\".", hs_proto);
713 * If a src_ipaddr is specified, when we send the return packet
714 * we will use this address instead of the src from the
718 #ifdef WITH_UDPFROMTO
719 if (ip_hton(cl_srcipaddr, c->ipaddr.af, &c->src_ipaddr) < 0) {
720 cf_log_err(cf_sectiontoitem(cs), "Failed parsing src_ipaddr");
724 DEBUGW("Server not build with udpfromto, ignoring client src_ipaddr");
730 if (c->prefix < 0) switch (c->ipaddr.af) {
741 #ifdef WITH_DYNAMIC_CLIENTS
742 if (c->client_server) {
744 c->secret = strdup("testing123");
746 if (((c->ipaddr.af == AF_INET) &&
747 (c->prefix == 32)) ||
748 ((c->ipaddr.af == AF_INET6) &&
749 (c->prefix == 128))) {
750 cf_log_err(cf_sectiontoitem(cs),
751 "Dynamic clients MUST be a network, not a single IP address.");
759 if (!c->secret || !*c->secret) {
761 const char *value = NULL;
762 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
764 if (cp) value = cf_pair_value(cp);
767 * Secrets aren't needed for DHCP.
769 if (value && (strcmp(value, "yes") == 0)) return c;
772 cf_log_err(cf_sectiontoitem(cs),
773 "secret must be at least 1 character long");
779 * Point the client to the home server pool, OR to the
780 * home server. This gets around the problem of figuring
781 * out which port to use.
784 c->coa_pool = home_pool_byname(c->coa_name, HOME_TYPE_COA);
786 c->coa_server = home_server_byname(c->coa_name,
789 if (!c->coa_pool && !c->coa_server) {
790 cf_log_err(cf_sectiontoitem(cs), "No such home_server or home_server_pool \"%s\"", c->coa_name);
797 if ((c->proto == IPPROTO_TCP) || (c->proto == IPPROTO_IP)) {
798 if ((c->limit.idle_timeout > 0) && (c->limit.idle_timeout < 5))
799 c->limit.idle_timeout = 5;
800 if ((c->limit.lifetime > 0) && (c->limit.lifetime < 5))
801 c->limit.lifetime = 5;
802 if ((c->limit.lifetime > 0) && (c->limit.idle_timeout > c->limit.lifetime))
803 c->limit.idle_timeout = 0;
812 * Create the linked list of clients from the new configuration
813 * type. This way we don't have to change too much in the other
816 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
818 int global = FALSE, in_server = FALSE;
821 RADCLIENT_LIST *clients;
824 * Be forgiving. If there's already a clients, return
825 * it. Otherwise create a new one.
827 clients = cf_data_find(section, "clients");
828 if (clients) return clients;
830 clients = clients_init();
831 if (!clients) return NULL;
833 if (cf_top_section(section) == section) global = TRUE;
835 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
837 for (cs = cf_subsection_find_next(section, NULL, "client");
839 cs = cf_subsection_find_next(section, cs, "client")) {
840 c = client_parse(cs, in_server);
846 * FIXME: Add the client as data via cf_data_add,
847 * for migration issues.
850 #ifdef WITH_DYNAMIC_CLIENTS
852 if (c->client_server) {
857 struct stat stat_buf;
861 * Find the directory where individual
862 * client definitions are stored.
864 cp = cf_pair_find(cs, "directory");
865 if (!cp) goto add_client;
867 value = cf_pair_value(cp);
869 cf_log_err(cf_sectiontoitem(cs),
870 "The \"directory\" entry must not be empty");
875 DEBUG("including dynamic clients in %s", value);
877 dir = opendir(value);
879 cf_log_err(cf_sectiontoitem(cs), "Error reading directory %s: %s", value, strerror(errno));
885 * Read the directory, ignoring "." files.
887 while ((dp = readdir(dir)) != NULL) {
891 if (dp->d_name[0] == '.') continue;
894 * Check for valid characters
896 for (p = dp->d_name; *p != '\0'; p++) {
897 if (isalpha((int)*p) ||
900 (*p == '.')) continue;
903 if (*p != '\0') continue;
905 snprintf(buf2, sizeof(buf2), "%s/%s",
908 if ((stat(buf2, &stat_buf) != 0) ||
909 S_ISDIR(stat_buf.st_mode)) continue;
911 dc = client_read(buf2, in_server, TRUE);
913 cf_log_err(cf_sectiontoitem(cs),
914 "Failed reading client file \"%s\"", buf2);
921 * Validate, and add to the list.
923 if (!client_validate(clients, c, dc)) {
929 } /* loop over the directory */
932 #endif /* HAVE_DIRENT_H */
933 #endif /* WITH_DYNAMIC_CLIENTS */
936 if (!client_add(clients, c)) {
937 cf_log_err(cf_sectiontoitem(cs),
938 "Failed to add client %s",
939 cf_section_name2(cs));
947 * Replace the global list of clients with the new one.
948 * The old one is still referenced from the original
949 * configuration, and will be freed when that is freed.
952 root_clients = clients;
958 #ifdef WITH_DYNAMIC_CLIENTS
960 * We overload this structure a lot.
962 static const CONF_PARSER dynamic_config[] = {
963 { "FreeRADIUS-Client-IP-Address", PW_TYPE_IPADDR,
964 offsetof(RADCLIENT, ipaddr), 0, NULL },
965 { "FreeRADIUS-Client-IPv6-Address", PW_TYPE_IPV6ADDR,
966 offsetof(RADCLIENT, ipaddr), 0, NULL },
967 { "FreeRADIUS-Client-Src-IP-Address", PW_TYPE_IPADDR,
968 offsetof(RADCLIENT, src_ipaddr), 0, NULL },
969 { "FreeRADIUS-Client-Src-IPv6-Address", PW_TYPE_IPV6ADDR,
970 offsetof(RADCLIENT, src_ipaddr), 0, NULL },
972 { "FreeRADIUS-Client-Require-MA", PW_TYPE_BOOLEAN,
973 offsetof(RADCLIENT, message_authenticator), NULL, NULL },
975 { "FreeRADIUS-Client-Secret", PW_TYPE_STRING_PTR,
976 offsetof(RADCLIENT, secret), 0, "" },
977 { "FreeRADIUS-Client-Shortname", PW_TYPE_STRING_PTR,
978 offsetof(RADCLIENT, shortname), 0, "" },
979 { "FreeRADIUS-Client-NAS-Type", PW_TYPE_STRING_PTR,
980 offsetof(RADCLIENT, nastype), 0, NULL },
981 { "FreeRADIUS-Client-Virtual-Server", PW_TYPE_STRING_PTR,
982 offsetof(RADCLIENT, server), 0, NULL },
984 { NULL, -1, 0, NULL, NULL }
988 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
993 * No virtual server defined. Inherit the parent's
996 if (master->server && !c->server) {
997 c->server = strdup(master->server);
1001 * If the client network isn't global (not tied to a
1002 * virtual server), then ensure that this clients server
1003 * is the same as the enclosing networks virtual server.
1005 if (master->server &&
1006 (strcmp(master->server, c->server) != 0)) {
1007 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
1009 buffer, sizeof(buffer)),
1015 if (!client_add(clients, c)) {
1016 DEBUG("- Cannot add client %s: Internal error",
1018 buffer, sizeof(buffer)));
1024 * Initialize the remaining fields.
1027 c->lifetime = master->lifetime;
1028 c->created = time(NULL);
1029 c->longname = strdup(c->shortname);
1031 DEBUG("- Added client %s with shared secret %s",
1032 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer)),
1043 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
1050 if (!clients || !request) return NULL;
1052 c = talloc_zero(clients, RADCLIENT);
1053 c->cs = request->client->cs;
1054 c->ipaddr.af = AF_UNSPEC;
1055 c->src_ipaddr.af = AF_UNSPEC;
1057 for (i = 0; dynamic_config[i].name != NULL; i++) {
1058 const DICT_ATTR *da;
1061 da = dict_attrbyname(dynamic_config[i].name);
1063 DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
1064 ip_ntoh(&request->packet->src_ipaddr,
1065 buffer, sizeof(buffer)),
1066 dynamic_config[i].name);
1072 vp = pairfind(request->config_items, da->attr, da->vendor, TAG_ANY);
1075 * Not required. Skip it.
1077 if (!dynamic_config[i].dflt) continue;
1079 DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",
1080 ip_ntoh(&request->packet->src_ipaddr,
1081 buffer, sizeof(buffer)),
1082 dynamic_config[i].name);
1086 switch (dynamic_config[i].type) {
1087 case PW_TYPE_IPADDR:
1088 if (da->type == PW_FREERADIUS_CLIENT_IP_ADDRESS) {
1089 c->ipaddr.af = AF_INET;
1090 c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1092 } else if (da->type == PW_FREERADIUS_CLIENT_SRC_IP_ADDRESS) {
1093 #ifdef WITH_UDPFROMTO
1094 c->src_ipaddr.af = AF_INET;
1095 c->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1097 DEBUGW("Server not build with udpfromto, ignoring FreeRADIUS-Client-Src-IP-Address.");
1103 case PW_TYPE_IPV6ADDR:
1104 if (da->type == PW_FREERADIUS_CLIENT_SRC_IPV6_ADDRESS) {
1105 c->ipaddr.af = AF_INET6;
1106 c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1108 } else if (da->type == PW_FREERADIUS_CLIENT_SRC_IPV6_ADDRESS) {
1109 #ifdef WITH_UDPFROMTO
1110 c->src_ipaddr.af = AF_INET6;
1111 c->src_ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1113 DEBUGW("Server not build with udpfromto, ignoring FreeRADIUS-Client-Src-IPv6-Address.");
1119 case PW_TYPE_STRING_PTR:
1120 p = (char **) ((char *) c + dynamic_config[i].offset);
1121 if (*p) talloc_free(*p);
1122 if (vp->vp_strvalue[0]) {
1123 *p = talloc_strdup(c->cs, vp->vp_strvalue);
1129 case PW_TYPE_BOOLEAN:
1130 pi = (int *) ((char *) c + dynamic_config[i].offset);
1131 *pi = vp->vp_integer;
1139 if (c->ipaddr.af == AF_UNSPEC) {
1140 DEBUG("- Cannot add client %s: No IP address was specified.",
1141 ip_ntoh(&request->packet->src_ipaddr,
1142 buffer, sizeof(buffer)));
1147 if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
1150 DEBUG("- Cannot add client %s: IP address %s do not match",
1151 ip_ntoh(&request->packet->src_ipaddr,
1152 buffer, sizeof(buffer)),
1154 buf2, sizeof(buf2)));
1158 if (!c->secret || !*c->secret) {
1159 DEBUG("- Cannot add client %s: No secret was specified.",
1160 ip_ntoh(&request->packet->src_ipaddr,
1161 buffer, sizeof(buffer)));
1165 if (!client_validate(clients, request->client, c)) {
1169 if ((c->src_ipaddr.af != AF_UNSPEC) && (c->src_ipaddr.af != c->ipaddr.af)) {
1170 DEBUG("- Cannot add client %s: Client IP and src address are different IP version.",
1171 ip_ntoh(&request->packet->src_ipaddr,
1172 buffer, sizeof(buffer)));
1181 * Read a client definition from the given filename.
1183 RADCLIENT *client_read(const char *filename, int in_server, int flag)
1190 if (!filename) return NULL;
1192 cs = cf_file_read(filename);
1193 if (!cs) return NULL;
1195 cs = cf_section_sub_find(cs, "client");
1197 radlog(L_ERR, "No \"client\" section found in client file");
1201 c = client_parse(cs, in_server);
1202 if (!c) return NULL;
1204 p = strrchr(filename, FR_DIR_SEP);
1211 if (!flag) return c;
1214 * Additional validations
1216 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
1217 if (strcmp(p, buffer) != 0) {
1218 DEBUG("Invalid client definition in %s: IP address %s does not match name %s", filename, buffer, p);