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)
66 #ifdef WITH_DYNAMIC_CLIENTS
67 if (client->dynamic == 2) {
70 if (!deleted_clients) {
71 deleted_clients = fr_fifo_create(1024,
72 (void *) client_free);
73 if (!deleted_clients) return; /* MEMLEAK */
77 * Mark it as in the fifo, and remember when we
81 client->created = now = time(NULL); /* re-set it */
82 fr_fifo_push(deleted_clients, client);
85 * Peek at the head of the fifo. If it might
86 * still be in use, return. Otherwise, pop it
87 * from the queue and delete it.
89 client = fr_fifo_peek(deleted_clients);
90 if ((client->created + 120) >= now) return;
92 client = fr_fifo_pop(deleted_clients);
93 rad_assert(client != NULL);
97 free(client->longname);
99 free(client->shortname);
100 free(client->nastype);
102 free(client->password);
103 free(client->server);
107 #ifdef WITH_ACCOUNTING
112 #ifdef WITH_DYNAMIC_CLIENTS
113 free(client->client_server);
120 * Callback for comparing two clients.
122 static int client_ipaddr_cmp(const void *one, const void *two)
124 const RADCLIENT *a = one;
125 const RADCLIENT *b = two;
127 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
131 static int client_num_cmp(const void *one, const void *two)
133 const RADCLIENT *a = one;
134 const RADCLIENT *b = two;
136 return (a->number - b->number);
141 * Free a RADCLIENT list.
143 void clients_free(RADCLIENT_LIST *clients)
147 if (!clients) return;
149 for (i = 0; i <= 128; i++) {
150 if (clients->trees[i]) rbtree_free(clients->trees[i]);
151 clients->trees[i] = NULL;
154 if (clients == root_clients) {
156 if (tree_num) rbtree_free(tree_num);
163 #ifdef WITH_DYNAMIC_CLIENTS
165 * FIXME: No fr_fifo_delete()
173 * Return a new, initialized, set of clients.
175 RADCLIENT_LIST *clients_init(void)
177 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
179 if (!clients) return NULL;
181 clients->min_prefix = 128;
188 * Sanity check a client.
190 static int client_sane(RADCLIENT *client)
192 switch (client->ipaddr.af) {
194 if (client->prefix > 32) {
199 * Zero out the subnet bits.
201 if (client->prefix == 0) {
202 memset(&client->ipaddr.ipaddr.ip4addr, 0,
203 sizeof(client->ipaddr.ipaddr.ip4addr));
205 } else if (client->prefix < 32) {
208 mask <<= (32 - client->prefix);
209 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
214 if (client->prefix > 128) return 0;
216 if (client->prefix == 0) {
217 memset(&client->ipaddr.ipaddr.ip6addr, 0,
218 sizeof(client->ipaddr.ipaddr.ip6addr));
220 } else if (client->prefix < 128) {
222 uint32_t mask, *addr;
224 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
226 for (i = client->prefix; i < 128; i += 32) {
228 mask <<= ((128 - i) & 0x1f);
229 addr[i / 32] &= mask;
243 * Add a client to the tree.
245 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
254 * If "clients" is NULL, it means add to the global list.
258 * Initialize it, if not done already.
261 root_clients = clients_init();
262 if (!root_clients) return 0;
264 clients = root_clients;
267 if ((client->prefix < 0) || (client->prefix > 128)) {
271 if (!client_sane(client)) return 0;
274 * Create a tree for it.
276 if (!clients->trees[client->prefix]) {
277 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
278 (void *) client_free, 0);
279 if (!clients->trees[client->prefix]) {
284 #define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0)))
287 * Cannot insert the same client twice.
289 old = rbtree_finddata(clients->trees[client->prefix], client);
292 * If it's a complete duplicate, then free the new
293 * one, and return "OK".
295 if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) &&
296 (old->prefix == client->prefix) &&
297 namecmp(longname) && namecmp(secret) &&
298 namecmp(shortname) && namecmp(nastype) &&
299 namecmp(login) && namecmp(password) && namecmp(server) &&
300 #ifdef WITH_DYNAMIC_CLIENTS
301 (old->lifetime == client->lifetime) &&
302 namecmp(client_server) &&
306 (old->coa_server == client->coa_server) &&
307 (old->coa_pool == client->coa_pool) &&
309 (old->message_authenticator == client->message_authenticator)) {
310 DEBUG("WARNING: Ignoring duplicate client %s", client->longname);
315 radlog(L_ERR, "Failed to add duplicate client %s",
322 * Other error adding client: likely is fatal.
324 if (!rbtree_insert(clients->trees[client->prefix], client)) {
330 tree_num = rbtree_create(client_num_cmp, NULL, 0);
335 * Catch clients added by rlm_sql.
338 client->auth = rad_malloc(sizeof(*client->auth));
339 memset(client->auth, 0, sizeof(*client->auth));
342 #ifdef WITH_ACCOUNTING
344 client->acct = rad_malloc(sizeof(*client->acct));
345 memset(client->acct, 0, sizeof(*client->acct));
349 #ifdef WITH_DYNAMIC_CLIENTS
351 * More catching of clients added by rlm_sql.
353 * The sql modules sets the dynamic flag BEFORE calling
354 * us. The client_create() function sets it AFTER
357 if (client->dynamic && (client->lifetime == 0)) {
361 * If there IS an enclosing network,
362 * inherit the lifetime from it.
364 network = client_find(clients, &client->ipaddr);
366 client->lifetime = network->lifetime;
371 client->number = tree_num_max;
373 if (tree_num) rbtree_insert(tree_num, client);
376 if (client->prefix < clients->min_prefix) {
377 clients->min_prefix = client->prefix;
384 #ifdef WITH_DYNAMIC_CLIENTS
385 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
387 if (!clients || !client) return;
389 rad_assert((client->prefix >= 0) && (client->prefix <= 128));
391 client->dynamic = 2; /* signal to client_free */
393 rbtree_deletebydata(tree_num, client);
394 rbtree_deletebydata(clients->trees[client->prefix], client);
400 * Find a client in the RADCLIENTS list by number.
401 * This is a support function for the statistics code.
403 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
407 if (!clients) clients = root_clients;
409 if (!clients) return NULL;
411 if (number >= tree_num_max) return NULL;
416 myclient.number = number;
418 return rbtree_finddata(tree_num, &myclient);
421 clients = clients; /* -Wunused */
422 number = number; /* -Wunused */
429 * Find a client in the RADCLIENTS list.
431 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
432 const fr_ipaddr_t *ipaddr)
437 if (!clients) clients = root_clients;
439 if (!clients || !ipaddr) return NULL;
441 switch (ipaddr->af) {
454 for (i = max_prefix; i >= clients->min_prefix; i--) {
458 myclient.ipaddr = *ipaddr;
459 client_sane(&myclient); /* clean up the ipaddress */
461 if (!clients->trees[i]) continue;
463 data = rbtree_finddata(clients->trees[i], &myclient);
474 * Old wrapper for client_find
476 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
478 return client_find(root_clients, ipaddr);
481 static struct in_addr cl_ip4addr;
482 static struct in6_addr cl_ip6addr;
484 static const CONF_PARSER client_config[] = {
485 { "ipaddr", PW_TYPE_IPADDR,
486 0, &cl_ip4addr, NULL },
487 { "ipv6addr", PW_TYPE_IPV6ADDR,
488 0, &cl_ip6addr, NULL },
489 { "netmask", PW_TYPE_INTEGER,
490 offsetof(RADCLIENT, prefix), 0, NULL },
492 { "require_message_authenticator", PW_TYPE_BOOLEAN,
493 offsetof(RADCLIENT, message_authenticator), 0, "no" },
495 { "secret", PW_TYPE_STRING_PTR,
496 offsetof(RADCLIENT, secret), 0, NULL },
497 { "shortname", PW_TYPE_STRING_PTR,
498 offsetof(RADCLIENT, shortname), 0, NULL },
499 { "nastype", PW_TYPE_STRING_PTR,
500 offsetof(RADCLIENT, nastype), 0, NULL },
501 { "login", PW_TYPE_STRING_PTR,
502 offsetof(RADCLIENT, login), 0, NULL },
503 { "password", PW_TYPE_STRING_PTR,
504 offsetof(RADCLIENT, password), 0, NULL },
505 { "virtual_server", PW_TYPE_STRING_PTR,
506 offsetof(RADCLIENT, server), 0, NULL },
507 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
508 offsetof(RADCLIENT, server), 0, NULL },
510 #ifdef WITH_DYNAMIC_CLIENTS
511 { "dynamic_clients", PW_TYPE_STRING_PTR,
512 offsetof(RADCLIENT, client_server), 0, NULL },
513 { "lifetime", PW_TYPE_INTEGER,
514 offsetof(RADCLIENT, lifetime), 0, NULL },
518 { "coa_server", PW_TYPE_STRING_PTR,
519 offsetof(RADCLIENT, coa_name), 0, NULL },
522 { NULL, -1, 0, NULL, NULL }
526 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
531 name2 = cf_section_name2(cs);
533 cf_log_err(cf_sectiontoitem(cs),
534 "Missing client name");
539 * The size is fine.. Let's create the buffer
541 c = rad_malloc(sizeof(*c));
542 memset(c, 0, sizeof(*c));
546 c->auth = rad_malloc(sizeof(*c->auth));
547 memset(c->auth, 0, sizeof(*c->auth));
549 #ifdef WITH_ACCOUNTING
550 c->acct = rad_malloc(sizeof(*c->acct));
551 memset(c->acct, 0, sizeof(*c->acct));
555 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
556 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
559 if (cf_section_parse(cs, c, client_config) < 0) {
561 cf_log_err(cf_sectiontoitem(cs),
562 "Error parsing client section.");
567 * Global clients can set servers to use,
568 * per-server clients cannot.
570 if (in_server && c->server) {
572 cf_log_err(cf_sectiontoitem(cs),
573 "Clients inside of an server section cannot point to a server.");
578 * No "ipaddr" or "ipv6addr", use old-style
579 * "client <ipaddr> {" syntax.
581 if (!cf_pair_find(cs, "ipaddr") &&
582 !cf_pair_find(cs, "ipv6addr")) {
585 prefix_ptr = strchr(name2, '/');
591 c->prefix = atoi(prefix_ptr + 1);
592 if ((c->prefix < 0) || (c->prefix > 128)) {
594 cf_log_err(cf_sectiontoitem(cs),
595 "Invalid Prefix value '%s' for IP.",
599 /* Replace '/' with '\0' */
604 * Always get the numeric representation of IP
606 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
608 cf_log_err(cf_sectiontoitem(cs),
609 "Failed to look up hostname %s: %s",
610 name2, fr_strerror());
614 if (prefix_ptr) *prefix_ptr = '/';
615 c->longname = strdup(name2);
617 if (!c->shortname) c->shortname = strdup(c->longname);
623 * Figure out which one to use.
625 if (cf_pair_find(cs, "ipaddr")) {
626 c->ipaddr.af = AF_INET;
627 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
629 if ((c->prefix < -1) || (c->prefix > 32)) {
631 cf_log_err(cf_sectiontoitem(cs),
632 "Netmask must be between 0 and 32");
636 } else if (cf_pair_find(cs, "ipv6addr")) {
637 c->ipaddr.af = AF_INET6;
638 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
640 if ((c->prefix < -1) || (c->prefix > 128)) {
642 cf_log_err(cf_sectiontoitem(cs),
643 "Netmask must be between 0 and 128");
647 cf_log_err(cf_sectiontoitem(cs),
648 "No IP address defined for the client");
653 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
654 c->longname = strdup(buffer);
657 * Set the short name to the name2
659 if (!c->shortname) c->shortname = strdup(name2);
662 if (c->prefix < 0) switch (c->ipaddr.af) {
673 #ifdef WITH_DYNAMIC_CLIENTS
674 if (c->client_server) {
676 c->secret = strdup("testing123");
678 if (((c->ipaddr.af == AF_INET) &&
679 (c->prefix == 32)) ||
680 ((c->ipaddr.af == AF_INET6) &&
681 (c->prefix == 128))) {
682 cf_log_err(cf_sectiontoitem(cs),
683 "Dynamic clients MUST be a network, not a single IP address.");
692 if (!c->secret || !*c->secret) {
694 const char *value = NULL;
695 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
697 if (cp) value = cf_pair_value(cp);
700 * Secrets aren't needed for DHCP.
702 if (value && (strcmp(value, "yes") == 0)) return c;
706 cf_log_err(cf_sectiontoitem(cs),
707 "secret must be at least 1 character long");
713 * Point the client to the home server pool, OR to the
714 * home server. This gets around the problem of figuring
715 * out which port to use.
718 c->coa_pool = home_pool_byname(c->coa_name, HOME_TYPE_COA);
720 c->coa_server = home_server_byname(c->coa_name,
723 if (!c->coa_pool && !c->coa_server) {
725 cf_log_err(cf_sectiontoitem(cs), "No such home_server or home_server_pool \"%s\"", c->coa_name);
736 * Create the linked list of clients from the new configuration
737 * type. This way we don't have to change too much in the other
740 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
742 int global = FALSE, in_server = FALSE;
745 RADCLIENT_LIST *clients;
748 * Be forgiving. If there's already a clients, return
749 * it. Otherwise create a new one.
751 clients = cf_data_find(section, "clients");
752 if (clients) return clients;
754 clients = clients_init();
755 if (!clients) return NULL;
757 if (cf_top_section(section) == section) global = TRUE;
759 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
762 * Associate the clients structure with the section, where
763 * it will be freed once the section is freed.
765 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
766 cf_log_err(cf_sectiontoitem(section),
767 "Failed to associate clients with section %s",
768 cf_section_name1(section));
769 clients_free(clients);
773 for (cs = cf_subsection_find_next(section, NULL, "client");
775 cs = cf_subsection_find_next(section, cs, "client")) {
776 c = client_parse(cs, in_server);
782 * FIXME: Add the client as data via cf_data_add,
783 * for migration issues.
786 #ifdef WITH_DYNAMIC_CLIENTS
788 if (c->client_server) {
793 struct stat stat_buf;
797 * Find the directory where individual
798 * client definitions are stored.
800 cp = cf_pair_find(cs, "directory");
801 if (!cp) goto add_client;
803 value = cf_pair_value(cp);
805 cf_log_err(cf_sectiontoitem(cs),
806 "The \"directory\" entry must not be empty");
811 DEBUG("including dynamic clients in %s", value);
813 dir = opendir(value);
815 cf_log_err(cf_sectiontoitem(cs), "Error reading directory %s: %s", value, strerror(errno));
821 * Read the directory, ignoring "." files.
823 while ((dp = readdir(dir)) != NULL) {
827 if (dp->d_name[0] == '.') continue;
830 * Check for valid characters
832 for (p = dp->d_name; *p != '\0'; p++) {
833 if (isalpha((int)*p) ||
836 (*p == '.')) continue;
839 if (*p != '\0') continue;
841 snprintf(buf2, sizeof(buf2), "%s/%s",
844 if ((stat(buf2, &stat_buf) != 0) ||
845 S_ISDIR(stat_buf.st_mode)) continue;
847 dc = client_read(buf2, in_server, TRUE);
849 cf_log_err(cf_sectiontoitem(cs),
850 "Failed reading client file \"%s\"", buf2);
856 * Validate, and add to the list.
858 if (!client_validate(clients, c, dc)) {
863 } /* loop over the directory */
865 #endif /* HAVE_DIRENT_H */
866 #endif /* WITH_DYNAMIC_CLIENTS */
869 if (!client_add(clients, c)) {
870 cf_log_err(cf_sectiontoitem(cs),
871 "Failed to add client %s",
872 cf_section_name2(cs));
880 * Replace the global list of clients with the new one.
881 * The old one is still referenced from the original
882 * configuration, and will be freed when that is freed.
885 root_clients = clients;
891 #ifdef WITH_DYNAMIC_CLIENTS
893 * We overload this structure a lot.
895 static const CONF_PARSER dynamic_config[] = {
896 { "FreeRADIUS-Client-IP-Address", PW_TYPE_IPADDR,
897 offsetof(RADCLIENT, ipaddr), 0, NULL },
898 { "FreeRADIUS-Client-IPv6-Address", PW_TYPE_IPV6ADDR,
899 offsetof(RADCLIENT, ipaddr), 0, NULL },
901 { "FreeRADIUS-Client-Require-MA", PW_TYPE_BOOLEAN,
902 offsetof(RADCLIENT, message_authenticator), NULL, NULL },
904 { "FreeRADIUS-Client-Secret", PW_TYPE_STRING_PTR,
905 offsetof(RADCLIENT, secret), 0, "" },
906 { "FreeRADIUS-Client-Shortname", PW_TYPE_STRING_PTR,
907 offsetof(RADCLIENT, shortname), 0, "" },
908 { "FreeRADIUS-Client-NAS-Type", PW_TYPE_STRING_PTR,
909 offsetof(RADCLIENT, nastype), 0, NULL },
910 { "FreeRADIUS-Client-Virtual-Server", PW_TYPE_STRING_PTR,
911 offsetof(RADCLIENT, server), 0, NULL },
913 { NULL, -1, 0, NULL, NULL }
917 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
922 * No virtual server defined. Inherit the parent's
925 if (master->server && !c->server) {
926 c->server = strdup(master->server);
930 * If the client network isn't global (not tied to a
931 * virtual server), then ensure that this clients server
932 * is the same as the enclosing networks virtual server.
934 if (master->server &&
935 (strcmp(master->server, c->server) != 0)) {
936 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
938 buffer, sizeof(buffer)),
944 if (!client_add(clients, c)) {
945 DEBUG("- Cannot add client %s: Internal error",
947 buffer, sizeof(buffer)));
953 * Initialize the remaining fields.
956 c->lifetime = master->lifetime;
957 c->created = time(NULL);
958 c->longname = strdup(c->shortname);
960 DEBUG("- Added client %s with shared secret %s",
961 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer)),
972 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
979 if (!clients || !request) return NULL;
981 c = rad_malloc(sizeof(*c));
982 memset(c, 0, sizeof(*c));
983 c->cs = request->client->cs;
984 c->ipaddr.af = AF_UNSPEC;
986 for (i = 0; dynamic_config[i].name != NULL; i++) {
990 da = dict_attrbyname(dynamic_config[i].name);
992 DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
993 ip_ntoh(&request->packet->src_ipaddr,
994 buffer, sizeof(buffer)),
995 dynamic_config[i].name);
1001 vp = pairfind(request->config_items, da->attr);
1004 * Not required. Skip it.
1006 if (!dynamic_config[i].dflt) continue;
1008 DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",
1009 ip_ntoh(&request->packet->src_ipaddr,
1010 buffer, sizeof(buffer)),
1011 dynamic_config[i].name);
1015 switch (dynamic_config[i].type) {
1016 case PW_TYPE_IPADDR:
1017 c->ipaddr.af = AF_INET;
1018 c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1022 case PW_TYPE_IPV6ADDR:
1023 c->ipaddr.af = AF_INET6;
1024 c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1028 case PW_TYPE_STRING_PTR:
1029 p = (char **) ((char *) c + dynamic_config[i].offset);
1031 *p = strdup(vp->vp_strvalue);
1034 case PW_TYPE_BOOLEAN:
1035 pi = (int *) ((char *) c + dynamic_config[i].offset);
1036 *pi = vp->vp_integer;
1044 if (c->ipaddr.af == AF_UNSPEC) {
1045 DEBUG("- Cannot add client %s: No IP address was specified.",
1046 ip_ntoh(&request->packet->src_ipaddr,
1047 buffer, sizeof(buffer)));
1052 if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
1055 DEBUG("- Cannot add client %s: IP address %s do not match",
1056 ip_ntoh(&request->packet->src_ipaddr,
1057 buffer, sizeof(buffer)),
1059 buf2, sizeof(buf2)));
1063 if (!client_validate(clients, request->client, c)) {
1071 * Read a client definition from the given filename.
1073 RADCLIENT *client_read(const char *filename, int in_server, int flag)
1080 if (!filename) return NULL;
1082 cs = cf_file_read(filename);
1083 if (!cs) return NULL;
1085 c = client_parse(cf_section_sub_find(cs, "client"), in_server);
1087 p = strrchr(filename, FR_DIR_SEP);
1094 if (!flag) return c;
1097 * Additional validations
1099 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
1100 if (strcmp(p, buffer) != 0) {
1101 DEBUG("Invalid client definition in %s: IP address %s does not match name %s", filename, buffer, p);