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; /* client numbers 0..N */
53 static int tree_num_max;
55 static RADCLIENT_LIST *root_clients;
58 * Callback for freeing a client.
60 void client_free(RADCLIENT *client)
62 free(client->longname);
64 free(client->shortname);
65 free(client->nastype);
67 free(client->password);
72 #ifdef WITH_ACCOUNTING
77 #ifdef WITH_DYNAMIC_CLIENTS
78 free(client->client_server);
85 * Callback for comparing two clients.
87 static int client_ipaddr_cmp(const void *one, const void *two)
89 const RADCLIENT *a = one;
90 const RADCLIENT *b = two;
92 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
96 static int client_num_cmp(const void *one, const void *two)
98 const RADCLIENT *a = one;
99 const RADCLIENT *b = two;
101 return (a->number - b->number);
106 * Free a RADCLIENT list.
108 void clients_free(RADCLIENT_LIST *clients)
112 if (!clients) return;
114 for (i = 0; i <= 128; i++) {
115 if (clients->trees[i]) rbtree_free(clients->trees[i]);
116 clients->trees[i] = NULL;
119 if (clients == root_clients) {
121 if (tree_num) rbtree_free(tree_num);
132 * Return a new, initialized, set of clients.
134 RADCLIENT_LIST *clients_init(void)
136 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
138 if (!clients) return NULL;
140 clients->min_prefix = 128;
147 * Sanity check a client.
149 static int client_sane(RADCLIENT *client)
151 switch (client->ipaddr.af) {
153 if (client->prefix > 32) {
158 * Zero out the subnet bits.
160 if (client->prefix == 0) {
161 memset(&client->ipaddr.ipaddr.ip4addr, 0,
162 sizeof(client->ipaddr.ipaddr.ip4addr));
164 } else if (client->prefix < 32) {
167 mask <<= (32 - client->prefix);
168 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
173 if (client->prefix > 128) return 0;
175 if (client->prefix == 0) {
176 memset(&client->ipaddr.ipaddr.ip6addr, 0,
177 sizeof(client->ipaddr.ipaddr.ip6addr));
179 } else if (client->prefix < 128) {
181 uint32_t mask, *addr;
183 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
185 for (i = client->prefix; i < 128; i += 32) {
187 mask <<= ((128 - i) & 0x1f);
188 addr[i / 32] &= mask;
202 * Add a client to the tree.
204 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
211 * If "clients" is NULL, it means add to the global list.
215 * Initialize it, if not done already.
218 root_clients = clients_init();
219 if (!root_clients) return 0;
221 clients = root_clients;
224 if ((client->prefix < 0) || (client->prefix > 128)) {
228 if (!client_sane(client)) return 0;
231 * Create a tree for it.
233 if (!clients->trees[client->prefix]) {
234 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
235 (void *) client_free, 0);
236 if (!clients->trees[client->prefix]) {
242 * Cannot insert the same client twice.
244 if (rbtree_find(clients->trees[client->prefix], client)) {
245 radlog(L_ERR, "Failed to add duplicate client %s",
251 * Other error adding client: likely is fatal.
253 if (!rbtree_insert(clients->trees[client->prefix], client)) {
259 tree_num = rbtree_create(client_num_cmp, NULL, 0);
264 * Catch clients added by rlm_sql.
267 client->auth = rad_malloc(sizeof(*client->auth));
268 memset(client->auth, 0, sizeof(*client->auth));
271 #ifdef WITH_ACCOUNTING
273 client->acct = rad_malloc(sizeof(*client->acct));
274 memset(client->acct, 0, sizeof(*client->acct));
278 #ifdef WITH_DYNAMIC_CLIENTS
280 * More catching of clients added by rlm_sql.
282 * The sql modules sets the dynamic flag BEFORE calling
283 * us. The client_create() function sets it AFTER
286 if (client->dynamic && (client->lifetime == 0)) {
290 * If there IS an enclosing network,
291 * inherit the lifetime from it.
293 network = client_find(clients, &client->ipaddr);
295 client->lifetime = network->lifetime;
300 client->number = tree_num_max;
302 if (tree_num) rbtree_insert(tree_num, client);
305 if (client->prefix < clients->min_prefix) {
306 clients->min_prefix = client->prefix;
313 #ifdef WITH_DYNAMIC_CLIENTS
314 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
316 if (!clients || !client) return;
318 rad_assert((client->prefix >= 0) && (client->prefix <= 128));
320 rbtree_deletebydata(clients->trees[client->prefix], client);
326 * Find a client in the RADCLIENTS list by number.
327 * This is a support function for the statistics code.
329 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
333 if (!clients) clients = root_clients;
335 if (!clients) return NULL;
337 if (number >= tree_num_max) return NULL;
342 myclient.number = number;
344 return rbtree_finddata(tree_num, &myclient);
347 clients = clients; /* -Wunused */
348 number = number; /* -Wunused */
355 * Find a client in the RADCLIENTS list.
357 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
358 const fr_ipaddr_t *ipaddr)
363 if (!clients) clients = root_clients;
365 if (!clients || !ipaddr) return NULL;
367 switch (ipaddr->af) {
380 for (i = max_prefix; i >= clients->min_prefix; i--) {
384 myclient.ipaddr = *ipaddr;
385 client_sane(&myclient); /* clean up the ipaddress */
387 if (!clients->trees[i]) continue;
389 data = rbtree_finddata(clients->trees[i], &myclient);
400 * Old wrapper for client_find
402 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
404 return client_find(root_clients, ipaddr);
407 static struct in_addr cl_ip4addr;
408 static struct in6_addr cl_ip6addr;
410 static const CONF_PARSER client_config[] = {
411 { "ipaddr", PW_TYPE_IPADDR,
412 0, &cl_ip4addr, NULL },
413 { "ipv6addr", PW_TYPE_IPV6ADDR,
414 0, &cl_ip6addr, NULL },
415 { "netmask", PW_TYPE_INTEGER,
416 offsetof(RADCLIENT, prefix), 0, NULL },
418 { "require_message_authenticator", PW_TYPE_BOOLEAN,
419 offsetof(RADCLIENT, message_authenticator), 0, "no" },
421 { "secret", PW_TYPE_STRING_PTR,
422 offsetof(RADCLIENT, secret), 0, NULL },
423 { "shortname", PW_TYPE_STRING_PTR,
424 offsetof(RADCLIENT, shortname), 0, NULL },
425 { "nastype", PW_TYPE_STRING_PTR,
426 offsetof(RADCLIENT, nastype), 0, NULL },
427 { "login", PW_TYPE_STRING_PTR,
428 offsetof(RADCLIENT, login), 0, NULL },
429 { "password", PW_TYPE_STRING_PTR,
430 offsetof(RADCLIENT, password), 0, NULL },
431 { "virtual_server", PW_TYPE_STRING_PTR,
432 offsetof(RADCLIENT, server), 0, NULL },
433 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
434 offsetof(RADCLIENT, server), 0, NULL },
436 #ifdef WITH_DYNAMIC_CLIENTS
437 { "dynamic_clients", PW_TYPE_STRING_PTR,
438 offsetof(RADCLIENT, client_server), 0, NULL },
439 { "lifetime", PW_TYPE_INTEGER,
440 offsetof(RADCLIENT, lifetime), 0, NULL },
443 { NULL, -1, 0, NULL, NULL }
447 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
452 name2 = cf_section_name2(cs);
454 cf_log_err(cf_sectiontoitem(cs),
455 "Missing client name");
460 * The size is fine.. Let's create the buffer
462 c = rad_malloc(sizeof(*c));
463 memset(c, 0, sizeof(*c));
467 c->auth = rad_malloc(sizeof(*c->auth));
468 memset(c->auth, 0, sizeof(*c->auth));
470 #ifdef WITH_ACCOUNTING
471 c->acct = rad_malloc(sizeof(*c->acct));
472 memset(c->acct, 0, sizeof(*c->acct));
476 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
477 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
480 if (cf_section_parse(cs, c, client_config) < 0) {
482 cf_log_err(cf_sectiontoitem(cs),
483 "Error parsing client section.");
488 * Global clients can set servers to use,
489 * per-server clients cannot.
491 if (in_server && c->server) {
493 cf_log_err(cf_sectiontoitem(cs),
494 "Clients inside of an server section cannot point to a server.");
499 * No "ipaddr" or "ipv6addr", use old-style
500 * "client <ipaddr> {" syntax.
502 if (!cf_pair_find(cs, "ipaddr") &&
503 !cf_pair_find(cs, "ipv6addr")) {
506 prefix_ptr = strchr(name2, '/');
512 c->prefix = atoi(prefix_ptr + 1);
513 if ((c->prefix < 0) || (c->prefix > 128)) {
515 cf_log_err(cf_sectiontoitem(cs),
516 "Invalid Prefix value '%s' for IP.",
520 /* Replace '/' with '\0' */
525 * Always get the numeric representation of IP
527 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
529 cf_log_err(cf_sectiontoitem(cs),
530 "Failed to look up hostname %s: %s",
531 name2, fr_strerror());
535 if (prefix_ptr) *prefix_ptr = '/';
536 c->longname = strdup(name2);
538 if (!c->shortname) c->shortname = strdup(c->longname);
544 * Figure out which one to use.
546 if (cf_pair_find(cs, "ipaddr")) {
547 c->ipaddr.af = AF_INET;
548 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
550 if ((c->prefix < -1) || (c->prefix > 32)) {
552 cf_log_err(cf_sectiontoitem(cs),
553 "Netmask must be between 0 and 32");
557 } else if (cf_pair_find(cs, "ipv6addr")) {
558 c->ipaddr.af = AF_INET6;
559 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
561 if ((c->prefix < -1) || (c->prefix > 128)) {
563 cf_log_err(cf_sectiontoitem(cs),
564 "Netmask must be between 0 and 128");
568 cf_log_err(cf_sectiontoitem(cs),
569 "No IP address defined for the client");
574 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
575 c->longname = strdup(buffer);
578 * Set the short name to the name2
580 if (!c->shortname) c->shortname = strdup(name2);
583 if (c->prefix < 0) switch (c->ipaddr.af) {
594 #ifdef WITH_DYNAMIC_CLIENTS
595 if (c->client_server) {
597 c->secret = strdup("testing123");
599 if (((c->ipaddr.af == AF_INET) &&
600 (c->prefix == 32)) ||
601 ((c->ipaddr.af == AF_INET6) &&
602 (c->prefix == 128))) {
603 cf_log_err(cf_sectiontoitem(cs),
604 "Dynamic clients MUST be a network, not a single IP address.");
613 if (!c->secret || !*c->secret) {
615 const char *value = NULL;
616 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
618 if (cp) value = cf_pair_value(cp);
621 * Secrets aren't needed for DHCP.
623 if (value && (strcmp(value, "yes") == 0)) return c;
627 cf_log_err(cf_sectiontoitem(cs),
628 "secret must be at least 1 character long");
638 * Create the linked list of clients from the new configuration
639 * type. This way we don't have to change too much in the other
642 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
644 int global = FALSE, in_server = FALSE;
647 RADCLIENT_LIST *clients;
650 * Be forgiving. If there's already a clients, return
651 * it. Otherwise create a new one.
653 clients = cf_data_find(section, "clients");
654 if (clients) return clients;
656 clients = clients_init();
657 if (!clients) return NULL;
659 if (cf_top_section(section) == section) global = TRUE;
661 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
664 * Associate the clients structure with the section, where
665 * it will be freed once the section is freed.
667 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
668 cf_log_err(cf_sectiontoitem(section),
669 "Failed to associate clients with section %s",
670 cf_section_name1(section));
671 clients_free(clients);
675 for (cs = cf_subsection_find_next(section, NULL, "client");
677 cs = cf_subsection_find_next(section, cs, "client")) {
678 c = client_parse(cs, in_server);
684 * FIXME: Add the client as data via cf_data_add,
685 * for migration issues.
688 #ifdef WITH_DYNAMIC_CLIENTS
690 if (c->client_server) {
695 struct stat stat_buf;
699 * Find the directory where individual
700 * client definitions are stored.
702 cp = cf_pair_find(cs, "directory");
705 value = cf_pair_value(cp);
707 cf_log_err(cf_sectiontoitem(cs),
708 "The \"directory\" entry must not be empty");
713 DEBUG("including dynamic clients in %s", value);
715 dir = opendir(value);
717 cf_log_err(cf_sectiontoitem(cs), "Error reading directory %s: %s", value, strerror(errno));
723 * Read the directory, ignoring "." files.
725 while ((dp = readdir(dir)) != NULL) {
729 if (dp->d_name[0] == '.') continue;
732 * Check for valid characters
734 for (p = dp->d_name; *p != '\0'; p++) {
735 if (isalpha((int)*p) ||
738 (*p == '.')) continue;
741 if (*p != '\0') continue;
743 snprintf(buf2, sizeof(buf2), "%s/%s",
746 if ((stat(buf2, &stat_buf) != 0) ||
747 S_ISDIR(stat_buf.st_mode)) continue;
749 dc = client_read(buf2, in_server);
751 cf_log_err(cf_sectiontoitem(cs),
752 "Failed reading client file \"%s\"", buf2);
758 * Validate, and add to the list.
760 if (!client_validate(clients, c, dc)) {
765 } /* loop over the directory */
767 #endif /* HAVE_DIRENT_H */
768 #endif /* WITH_DYNAMIC_CLIENTS */
770 if (!client_add(clients, c)) {
771 cf_log_err(cf_sectiontoitem(cs),
772 "Failed to add client %s",
773 cf_section_name2(cs));
781 * Replace the global list of clients with the new one.
782 * The old one is still referenced from the original
783 * configuration, and will be freed when that is freed.
786 root_clients = clients;
792 #ifdef WITH_DYNAMIC_CLIENTS
794 * We overload this structure a lot.
796 static const CONF_PARSER dynamic_config[] = {
797 { "FreeRADIUS-Client-IP-Address", PW_TYPE_IPADDR,
798 offsetof(RADCLIENT, ipaddr), 0, NULL },
799 { "FreeRADIUS-Client-IPv6-Address", PW_TYPE_IPV6ADDR,
800 offsetof(RADCLIENT, ipaddr), 0, NULL },
802 { "FreeRADIUS-Client-Require-MA", PW_TYPE_BOOLEAN,
803 offsetof(RADCLIENT, message_authenticator), NULL, NULL },
805 { "FreeRADIUS-Client-Secret", PW_TYPE_STRING_PTR,
806 offsetof(RADCLIENT, secret), 0, "" },
807 { "FreeRADIUS-Client-Shortname", PW_TYPE_STRING_PTR,
808 offsetof(RADCLIENT, shortname), 0, "" },
809 { "FreeRADIUS-Client-NAS-Type", PW_TYPE_STRING_PTR,
810 offsetof(RADCLIENT, nastype), 0, NULL },
811 { "FreeRADIUS-Client-Virtual-Server", PW_TYPE_STRING_PTR,
812 offsetof(RADCLIENT, server), 0, NULL },
814 { NULL, -1, 0, NULL, NULL }
818 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
823 * No virtual server defined. Inherit the parent's
826 if (master->server && !c->server) {
827 c->server = strdup(master->server);
831 * If the client network isn't global (not tied to a
832 * virtual server), then ensure that this clients server
833 * is the same as the enclosing networks virtual server.
835 if (master->server &&
836 (strcmp(master->server, c->server) != 0)) {
837 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
839 buffer, sizeof(buffer)),
845 if (!client_add(clients, c)) {
846 DEBUG("- Cannot add client %s: Internal error",
848 buffer, sizeof(buffer)));
854 * Initialize the remaining fields.
857 c->lifetime = master->lifetime;
858 c->created = time(NULL);
859 c->longname = strdup(c->shortname);
861 DEBUG("- Added client %s with shared secret %s",
862 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer)),
873 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
880 if (!clients || !request) return NULL;
882 c = rad_malloc(sizeof(*c));
883 memset(c, 0, sizeof(*c));
884 c->cs = request->client->cs;
885 c->ipaddr.af = AF_UNSPEC;
887 for (i = 0; dynamic_config[i].name != NULL; i++) {
891 da = dict_attrbyname(dynamic_config[i].name);
893 DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
894 ip_ntoh(&request->packet->src_ipaddr,
895 buffer, sizeof(buffer)),
896 dynamic_config[i].name);
902 vp = pairfind(request->config_items, da->attr);
905 * Not required. Skip it.
907 if (!dynamic_config[i].dflt) continue;
909 DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",
910 ip_ntoh(&request->packet->src_ipaddr,
911 buffer, sizeof(buffer)),
912 dynamic_config[i].name);
916 switch (dynamic_config[i].type) {
918 c->ipaddr.af = AF_INET;
919 c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
923 case PW_TYPE_IPV6ADDR:
924 c->ipaddr.af = AF_INET6;
925 c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
929 case PW_TYPE_STRING_PTR:
930 p = (char **) ((char *) c + dynamic_config[i].offset);
932 *p = strdup(vp->vp_strvalue);
935 case PW_TYPE_BOOLEAN:
936 pi = (int *) ((char *) c + dynamic_config[i].offset);
937 *pi = vp->vp_integer;
945 if (c->ipaddr.af == AF_UNSPEC) {
946 DEBUG("- Cannot add client %s: No IP address was specified.",
947 ip_ntoh(&request->packet->src_ipaddr,
948 buffer, sizeof(buffer)));
953 if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
956 DEBUG("- Cannot add client %s: IP address %s do not match",
957 ip_ntoh(&request->packet->src_ipaddr,
958 buffer, sizeof(buffer)),
960 buf2, sizeof(buf2)));
964 if (!client_validate(clients, request->client, c)) {
972 * Read a client definition from the given filename.
974 RADCLIENT *client_read(const char *filename, int in_server)
981 if (!filename) return NULL;
983 cs = cf_file_read(filename);
984 if (!cs) return NULL;
986 c = client_parse(cf_section_sub_find(cs, "client"), in_server);
988 p = strrchr(filename, FR_DIR_SEP);
996 * Additional validations
998 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
999 if (strcmp(p, buffer) != 0) {
1000 DEBUG("Invalid client definition in %s: IP address %s does not match name %s", filename, buffer, p);