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 struct radclient_list {
38 * FIXME: One set of trees for IPv4, and another for IPv6?
40 rbtree_t *trees[129]; /* for 0..128, inclusive. */
46 static rbtree_t *tree_num; /* client numbers 0..N */
47 static int tree_num_max;
49 static RADCLIENT_LIST *root_clients;
52 * Callback for freeing a client.
54 void client_free(RADCLIENT *client)
56 free(client->longname);
58 free(client->shortname);
59 free(client->nastype);
61 free(client->password);
66 #ifdef WITH_ACCOUNTING
71 #ifdef WITH_DYNAMIC_CLIENTS
72 free(client->client_server);
79 * Callback for comparing two clients.
81 static int client_ipaddr_cmp(const void *one, const void *two)
83 const RADCLIENT *a = one;
84 const RADCLIENT *b = two;
86 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
90 static int client_num_cmp(const void *one, const void *two)
92 const RADCLIENT *a = one;
93 const RADCLIENT *b = two;
95 return (a->number - b->number);
100 * Free a RADCLIENT list.
102 void clients_free(RADCLIENT_LIST *clients)
106 if (!clients) return;
108 for (i = 0; i <= 128; i++) {
109 if (clients->trees[i]) rbtree_free(clients->trees[i]);
110 clients->trees[i] = NULL;
113 if (clients == root_clients) {
115 if (tree_num) rbtree_free(tree_num);
126 * Return a new, initialized, set of clients.
128 RADCLIENT_LIST *clients_init(void)
130 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
132 if (!clients) return NULL;
134 clients->min_prefix = 128;
141 * Sanity check a client.
143 static int client_sane(RADCLIENT *client)
145 switch (client->ipaddr.af) {
147 if (client->prefix > 32) {
152 * Zero out the subnet bits.
154 if (client->prefix == 0) {
155 memset(&client->ipaddr.ipaddr.ip4addr, 0,
156 sizeof(client->ipaddr.ipaddr.ip4addr));
158 } else if (client->prefix < 32) {
161 mask <<= (32 - client->prefix);
162 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
167 if (client->prefix > 128) return 0;
169 if (client->prefix == 0) {
170 memset(&client->ipaddr.ipaddr.ip6addr, 0,
171 sizeof(client->ipaddr.ipaddr.ip6addr));
173 } else if (client->prefix < 128) {
175 uint32_t mask, *addr;
177 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
179 for (i = client->prefix; i < 128; i += 32) {
181 mask <<= ((128 - i) & 0x1f);
182 addr[i / 32] &= mask;
196 * Add a client to the tree.
198 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
205 * If "clients" is NULL, it means add to the global list.
209 * Initialize it, if not done already.
212 root_clients = clients_init();
213 if (!root_clients) return 0;
215 clients = root_clients;
218 if ((client->prefix < 0) || (client->prefix > 128)) {
222 if (!client_sane(client)) return 0;
225 * Create a tree for it.
227 if (!clients->trees[client->prefix]) {
228 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
229 (void *) client_free, 0);
230 if (!clients->trees[client->prefix]) {
236 * Cannot insert the same client twice.
238 if (rbtree_find(clients->trees[client->prefix], client)) {
239 radlog(L_ERR, "Failed to add duplicate client %s",
245 * Other error adding client: likely is fatal.
247 if (!rbtree_insert(clients->trees[client->prefix], client)) {
253 tree_num = rbtree_create(client_num_cmp, NULL, 0);
258 * Catch clients added by rlm_sql.
261 client->auth = rad_malloc(sizeof(*client->auth));
262 memset(client->auth, 0, sizeof(*client->auth));
265 #ifdef WITH_ACCOUNTING
267 client->acct = rad_malloc(sizeof(*client->acct));
268 memset(client->acct, 0, sizeof(*client->acct));
272 #ifdef WITH_DYNAMIC_CLIENTS
274 * More catching of clients added by rlm_sql.
276 * The sql modules sets the dynamic flag BEFORE calling
277 * us. The client_create() function sets it AFTER
280 if (client->dynamic && (client->lifetime == 0)) {
284 * If there IS an enclosing network,
285 * inherit the lifetime from it.
287 network = client_find(clients, &client->ipaddr);
289 client->lifetime = network->lifetime;
294 client->number = tree_num_max;
296 if (tree_num) rbtree_insert(tree_num, client);
299 if (client->prefix < clients->min_prefix) {
300 clients->min_prefix = client->prefix;
307 #ifdef WITH_DYNAMIC_CLIENTS
308 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
310 if (!clients || !client) return;
312 rad_assert((client->prefix >= 0) && (client->prefix <= 128));
314 rbtree_deletebydata(clients->trees[client->prefix], client);
320 * Find a client in the RADCLIENTS list by number.
321 * This is a support function for the statistics code.
323 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
327 if (!clients) clients = root_clients;
329 if (!clients) return NULL;
331 if (number >= tree_num_max) return NULL;
336 myclient.number = number;
338 return rbtree_finddata(tree_num, &myclient);
341 clients = clients; /* -Wunused */
342 number = number; /* -Wunused */
349 * Find a client in the RADCLIENTS list.
351 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
352 const fr_ipaddr_t *ipaddr)
357 if (!clients) clients = root_clients;
359 if (!clients || !ipaddr) return NULL;
361 switch (ipaddr->af) {
374 for (i = max_prefix; i >= clients->min_prefix; i--) {
378 myclient.ipaddr = *ipaddr;
379 client_sane(&myclient); /* clean up the ipaddress */
381 if (!clients->trees[i]) continue;
383 data = rbtree_finddata(clients->trees[i], &myclient);
394 * Old wrapper for client_find
396 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
398 return client_find(root_clients, ipaddr);
401 static struct in_addr cl_ip4addr;
402 static struct in6_addr cl_ip6addr;
404 static const CONF_PARSER client_config[] = {
405 { "ipaddr", PW_TYPE_IPADDR,
406 0, &cl_ip4addr, NULL },
407 { "ipv6addr", PW_TYPE_IPV6ADDR,
408 0, &cl_ip6addr, NULL },
409 { "netmask", PW_TYPE_INTEGER,
410 offsetof(RADCLIENT, prefix), 0, NULL },
412 { "require_message_authenticator", PW_TYPE_BOOLEAN,
413 offsetof(RADCLIENT, message_authenticator), 0, "no" },
415 { "secret", PW_TYPE_STRING_PTR,
416 offsetof(RADCLIENT, secret), 0, NULL },
417 { "shortname", PW_TYPE_STRING_PTR,
418 offsetof(RADCLIENT, shortname), 0, NULL },
419 { "nastype", PW_TYPE_STRING_PTR,
420 offsetof(RADCLIENT, nastype), 0, NULL },
421 { "login", PW_TYPE_STRING_PTR,
422 offsetof(RADCLIENT, login), 0, NULL },
423 { "password", PW_TYPE_STRING_PTR,
424 offsetof(RADCLIENT, password), 0, NULL },
425 { "virtual_server", PW_TYPE_STRING_PTR,
426 offsetof(RADCLIENT, server), 0, NULL },
427 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
428 offsetof(RADCLIENT, server), 0, NULL },
430 #ifdef WITH_DYNAMIC_CLIENTS
431 { "dynamic_clients", PW_TYPE_STRING_PTR,
432 offsetof(RADCLIENT, client_server), 0, NULL },
433 { "lifetime", PW_TYPE_INTEGER,
434 offsetof(RADCLIENT, lifetime), 0, NULL },
437 { NULL, -1, 0, NULL, NULL }
441 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
446 name2 = cf_section_name2(cs);
448 cf_log_err(cf_sectiontoitem(cs),
449 "Missing client name");
454 * The size is fine.. Let's create the buffer
456 c = rad_malloc(sizeof(*c));
457 memset(c, 0, sizeof(*c));
461 c->auth = rad_malloc(sizeof(*c->auth));
462 memset(c->auth, 0, sizeof(*c->auth));
464 #ifdef WITH_ACCOUNTING
465 c->acct = rad_malloc(sizeof(*c->acct));
466 memset(c->acct, 0, sizeof(*c->acct));
470 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
471 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
474 if (cf_section_parse(cs, c, client_config) < 0) {
476 cf_log_err(cf_sectiontoitem(cs),
477 "Error parsing client section.");
482 * Global clients can set servers to use,
483 * per-server clients cannot.
485 if (in_server && c->server) {
487 cf_log_err(cf_sectiontoitem(cs),
488 "Clients inside of an server section cannot point to a server.");
493 * No "ipaddr" or "ipv6addr", use old-style
494 * "client <ipaddr> {" syntax.
496 if (!cf_pair_find(cs, "ipaddr") &&
497 !cf_pair_find(cs, "ipv6addr")) {
500 prefix_ptr = strchr(name2, '/');
506 c->prefix = atoi(prefix_ptr + 1);
507 if ((c->prefix < 0) || (c->prefix > 128)) {
509 cf_log_err(cf_sectiontoitem(cs),
510 "Invalid Prefix value '%s' for IP.",
514 /* Replace '/' with '\0' */
519 * Always get the numeric representation of IP
521 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
523 cf_log_err(cf_sectiontoitem(cs),
524 "Failed to look up hostname %s: %s",
525 name2, fr_strerror());
529 if (prefix_ptr) *prefix_ptr = '/';
530 c->longname = strdup(name2);
532 if (!c->shortname) c->shortname = strdup(c->longname);
538 * Figure out which one to use.
540 if (cf_pair_find(cs, "ipaddr")) {
541 c->ipaddr.af = AF_INET;
542 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
544 if ((c->prefix < -1) || (c->prefix > 32)) {
546 cf_log_err(cf_sectiontoitem(cs),
547 "Netmask must be between 0 and 32");
551 } else if (cf_pair_find(cs, "ipv6addr")) {
552 c->ipaddr.af = AF_INET6;
553 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
555 if ((c->prefix < -1) || (c->prefix > 128)) {
557 cf_log_err(cf_sectiontoitem(cs),
558 "Netmask must be between 0 and 128");
562 cf_log_err(cf_sectiontoitem(cs),
563 "No IP address defined for the client");
568 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
569 c->longname = strdup(buffer);
572 * Set the short name to the name2
574 if (!c->shortname) c->shortname = strdup(name2);
577 if (c->prefix < 0) switch (c->ipaddr.af) {
588 #ifdef WITH_DYNAMIC_CLIENTS
589 if (c->client_server) {
591 c->secret = strdup("testing123");
593 if (((c->ipaddr.af == AF_INET) &&
594 (c->prefix == 32)) ||
595 ((c->ipaddr.af == AF_INET6) &&
596 (c->prefix == 128))) {
597 cf_log_err(cf_sectiontoitem(cs),
598 "Dynamic clients MUST be a network, not a single IP address.");
607 if (!c->secret || !*c->secret) {
609 const char *value = NULL;
610 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
612 if (cp) value = cf_pair_value(cp);
615 * Secrets aren't needed for DHCP.
617 if (value && (strcmp(value, "yes") == 0)) return c;
621 cf_log_err(cf_sectiontoitem(cs),
622 "secret must be at least 1 character long");
631 * Create the linked list of clients from the new configuration
632 * type. This way we don't have to change too much in the other
635 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
637 int global = FALSE, in_server = FALSE;
640 RADCLIENT_LIST *clients;
643 * Be forgiving. If there's already a clients, return
644 * it. Otherwise create a new one.
646 clients = cf_data_find(section, "clients");
647 if (clients) return clients;
649 clients = clients_init();
650 if (!clients) return NULL;
652 if (cf_top_section(section) == section) global = TRUE;
654 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
657 * Associate the clients structure with the section, where
658 * it will be freed once the section is freed.
660 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
661 cf_log_err(cf_sectiontoitem(section),
662 "Failed to associate clients with section %s",
663 cf_section_name1(section));
664 clients_free(clients);
668 for (cs = cf_subsection_find_next(section, NULL, "client");
670 cs = cf_subsection_find_next(section, cs, "client")) {
671 c = client_parse(cs, in_server);
677 * FIXME: Add the client as data via cf_data_add,
678 * for migration issues.
681 if (!client_add(clients, c)) {
682 cf_log_err(cf_sectiontoitem(cs),
683 "Failed to add client %s",
684 cf_section_name2(cs));
691 * Replace the global list of clients with the new one.
692 * The old one is still referenced from the original
693 * configuration, and will be freed when that is freed.
696 root_clients = clients;
702 #ifdef WITH_DYNAMIC_CLIENTS
704 * We overload this structure a lot.
706 static const CONF_PARSER dynamic_config[] = {
707 { "FreeRADIUS-Client-IP-Address", PW_TYPE_IPADDR,
708 offsetof(RADCLIENT, ipaddr), 0, NULL },
709 { "FreeRADIUS-Client-IPv6-Address", PW_TYPE_IPV6ADDR,
710 offsetof(RADCLIENT, ipaddr), 0, NULL },
712 { "FreeRADIUS-Client-Require-MA", PW_TYPE_BOOLEAN,
713 offsetof(RADCLIENT, message_authenticator), NULL, NULL },
715 { "FreeRADIUS-Client-Secret", PW_TYPE_STRING_PTR,
716 offsetof(RADCLIENT, secret), 0, "" },
717 { "FreeRADIUS-Client-Shortname", PW_TYPE_STRING_PTR,
718 offsetof(RADCLIENT, shortname), 0, "" },
719 { "FreeRADIUS-Client-NAS-Type", PW_TYPE_STRING_PTR,
720 offsetof(RADCLIENT, nastype), 0, NULL },
721 { "FreeRADIUS-Client-Virtual-Server", PW_TYPE_STRING_PTR,
722 offsetof(RADCLIENT, server), 0, NULL },
724 { NULL, -1, 0, NULL, NULL }
727 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
734 if (!clients || !request) return NULL;
736 c = rad_malloc(sizeof(*c));
737 memset(c, 0, sizeof(*c));
738 c->cs = request->client->cs;
739 c->ipaddr.af = AF_UNSPEC;
741 for (i = 0; dynamic_config[i].name != NULL; i++) {
745 da = dict_attrbyname(dynamic_config[i].name);
747 DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
748 ip_ntoh(&request->packet->src_ipaddr,
749 buffer, sizeof(buffer)),
750 dynamic_config[i].name);
756 vp = pairfind(request->config_items, da->attr);
759 * Not required. Skip it.
761 if (!dynamic_config[i].dflt) continue;
763 DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",
764 ip_ntoh(&request->packet->src_ipaddr,
765 buffer, sizeof(buffer)),
766 dynamic_config[i].name);
770 switch (dynamic_config[i].type) {
772 c->ipaddr.af = AF_INET;
773 c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
777 case PW_TYPE_IPV6ADDR:
778 c->ipaddr.af = AF_INET6;
779 c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
783 case PW_TYPE_STRING_PTR:
784 p = (char **) ((char *) c + dynamic_config[i].offset);
786 *p = strdup(vp->vp_strvalue);
789 case PW_TYPE_BOOLEAN:
790 pi = (int *) ((char *) c + dynamic_config[i].offset);
791 *pi = vp->vp_integer;
799 if (c->ipaddr.af == AF_UNSPEC) {
800 DEBUG("- Cannot add client %s: No IP address was specified.",
801 ip_ntoh(&request->packet->src_ipaddr,
802 buffer, sizeof(buffer)));
807 if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
810 DEBUG("- Cannot add client %s: IP address %s do not match",
811 ip_ntoh(&request->packet->src_ipaddr,
812 buffer, sizeof(buffer)),
814 buf2, sizeof(buf2)));
819 * No virtual server defined. Inherit the parent's
822 if (request->client->server && !c->server) {
823 c->server = strdup(request->client->server);
827 * If the client network isn't global (not tied to a
828 * virtual server), then ensure that this clients server
829 * is the same as the enclosing networks virtual server.
831 if (request->client->server &&
832 (strcmp(request->client->server, c->server) != 0)) {
833 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
834 ip_ntoh(&request->packet->src_ipaddr,
835 buffer, sizeof(buffer)),
841 if (!client_add(clients, c)) {
842 DEBUG("- Cannot add client %s: Internal error",
843 ip_ntoh(&request->packet->src_ipaddr,
844 buffer, sizeof(buffer)));
850 * Initialize the remaining fields.
853 c->lifetime = request->client->lifetime;
854 c->created = request->timestamp;
855 c->longname = strdup(c->shortname);
857 DEBUG("- Added client %s with shared secret %s",
858 ip_ntoh(&request->packet->src_ipaddr, buffer, sizeof(buffer)),