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 },
444 { "coa_server", PW_TYPE_STRING_PTR,
445 offsetof(RADCLIENT, coa_name), 0, NULL },
448 { NULL, -1, 0, NULL, NULL }
452 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
457 name2 = cf_section_name2(cs);
459 cf_log_err(cf_sectiontoitem(cs),
460 "Missing client name");
465 * The size is fine.. Let's create the buffer
467 c = rad_malloc(sizeof(*c));
468 memset(c, 0, sizeof(*c));
472 c->auth = rad_malloc(sizeof(*c->auth));
473 memset(c->auth, 0, sizeof(*c->auth));
475 #ifdef WITH_ACCOUNTING
476 c->acct = rad_malloc(sizeof(*c->acct));
477 memset(c->acct, 0, sizeof(*c->acct));
481 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
482 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
485 if (cf_section_parse(cs, c, client_config) < 0) {
487 cf_log_err(cf_sectiontoitem(cs),
488 "Error parsing client section.");
493 * Global clients can set servers to use,
494 * per-server clients cannot.
496 if (in_server && c->server) {
498 cf_log_err(cf_sectiontoitem(cs),
499 "Clients inside of an server section cannot point to a server.");
504 * No "ipaddr" or "ipv6addr", use old-style
505 * "client <ipaddr> {" syntax.
507 if (!cf_pair_find(cs, "ipaddr") &&
508 !cf_pair_find(cs, "ipv6addr")) {
511 prefix_ptr = strchr(name2, '/');
517 c->prefix = atoi(prefix_ptr + 1);
518 if ((c->prefix < 0) || (c->prefix > 128)) {
520 cf_log_err(cf_sectiontoitem(cs),
521 "Invalid Prefix value '%s' for IP.",
525 /* Replace '/' with '\0' */
530 * Always get the numeric representation of IP
532 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
534 cf_log_err(cf_sectiontoitem(cs),
535 "Failed to look up hostname %s: %s",
536 name2, fr_strerror());
540 if (prefix_ptr) *prefix_ptr = '/';
541 c->longname = strdup(name2);
543 if (!c->shortname) c->shortname = strdup(c->longname);
549 * Figure out which one to use.
551 if (cf_pair_find(cs, "ipaddr")) {
552 c->ipaddr.af = AF_INET;
553 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
555 if ((c->prefix < -1) || (c->prefix > 32)) {
557 cf_log_err(cf_sectiontoitem(cs),
558 "Netmask must be between 0 and 32");
562 } else if (cf_pair_find(cs, "ipv6addr")) {
563 c->ipaddr.af = AF_INET6;
564 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
566 if ((c->prefix < -1) || (c->prefix > 128)) {
568 cf_log_err(cf_sectiontoitem(cs),
569 "Netmask must be between 0 and 128");
573 cf_log_err(cf_sectiontoitem(cs),
574 "No IP address defined for the client");
579 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
580 c->longname = strdup(buffer);
583 * Set the short name to the name2
585 if (!c->shortname) c->shortname = strdup(name2);
588 if (c->prefix < 0) switch (c->ipaddr.af) {
599 #ifdef WITH_DYNAMIC_CLIENTS
600 if (c->client_server) {
602 c->secret = strdup("testing123");
604 if (((c->ipaddr.af == AF_INET) &&
605 (c->prefix == 32)) ||
606 ((c->ipaddr.af == AF_INET6) &&
607 (c->prefix == 128))) {
608 cf_log_err(cf_sectiontoitem(cs),
609 "Dynamic clients MUST be a network, not a single IP address.");
618 if (!c->secret || !*c->secret) {
620 const char *value = NULL;
621 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
623 if (cp) value = cf_pair_value(cp);
626 * Secrets aren't needed for DHCP.
628 if (value && (strcmp(value, "yes") == 0)) return c;
632 cf_log_err(cf_sectiontoitem(cs),
633 "secret must be at least 1 character long");
639 * Point the client to the home server pool, OR to the
640 * home server. This gets around the problem of figuring
641 * out which port to use.
644 c->coa_pool = home_pool_byname(c->coa_name, HOME_TYPE_COA);
646 c->coa_server = home_server_byname(c->coa_name);
648 if (!c->coa_server) {
650 cf_log_err(cf_sectiontoitem(cs), "No such home_server or home_server_pool \"%s\"", c->coa_name);
661 * Create the linked list of clients from the new configuration
662 * type. This way we don't have to change too much in the other
665 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
667 int global = FALSE, in_server = FALSE;
670 RADCLIENT_LIST *clients;
673 * Be forgiving. If there's already a clients, return
674 * it. Otherwise create a new one.
676 clients = cf_data_find(section, "clients");
677 if (clients) return clients;
679 clients = clients_init();
680 if (!clients) return NULL;
682 if (cf_top_section(section) == section) global = TRUE;
684 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
687 * Associate the clients structure with the section, where
688 * it will be freed once the section is freed.
690 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
691 cf_log_err(cf_sectiontoitem(section),
692 "Failed to associate clients with section %s",
693 cf_section_name1(section));
694 clients_free(clients);
698 for (cs = cf_subsection_find_next(section, NULL, "client");
700 cs = cf_subsection_find_next(section, cs, "client")) {
701 c = client_parse(cs, in_server);
707 * FIXME: Add the client as data via cf_data_add,
708 * for migration issues.
711 #ifdef WITH_DYNAMIC_CLIENTS
713 if (c->client_server) {
718 struct stat stat_buf;
722 * Find the directory where individual
723 * client definitions are stored.
725 cp = cf_pair_find(cs, "directory");
726 if (!cp) goto add_client;
728 value = cf_pair_value(cp);
730 cf_log_err(cf_sectiontoitem(cs),
731 "The \"directory\" entry must not be empty");
736 DEBUG("including dynamic clients in %s", value);
738 dir = opendir(value);
740 cf_log_err(cf_sectiontoitem(cs), "Error reading directory %s: %s", value, strerror(errno));
746 * Read the directory, ignoring "." files.
748 while ((dp = readdir(dir)) != NULL) {
752 if (dp->d_name[0] == '.') continue;
755 * Check for valid characters
757 for (p = dp->d_name; *p != '\0'; p++) {
758 if (isalpha((int)*p) ||
761 (*p == '.')) continue;
764 if (*p != '\0') continue;
766 snprintf(buf2, sizeof(buf2), "%s/%s",
769 if ((stat(buf2, &stat_buf) != 0) ||
770 S_ISDIR(stat_buf.st_mode)) continue;
772 dc = client_read(buf2, in_server, TRUE);
774 cf_log_err(cf_sectiontoitem(cs),
775 "Failed reading client file \"%s\"", buf2);
781 * Validate, and add to the list.
783 if (!client_validate(clients, c, dc)) {
788 } /* loop over the directory */
790 #endif /* HAVE_DIRENT_H */
791 #endif /* WITH_DYNAMIC_CLIENTS */
794 if (!client_add(clients, c)) {
795 cf_log_err(cf_sectiontoitem(cs),
796 "Failed to add client %s",
797 cf_section_name2(cs));
805 * Replace the global list of clients with the new one.
806 * The old one is still referenced from the original
807 * configuration, and will be freed when that is freed.
810 root_clients = clients;
816 #ifdef WITH_DYNAMIC_CLIENTS
818 * We overload this structure a lot.
820 static const CONF_PARSER dynamic_config[] = {
821 { "FreeRADIUS-Client-IP-Address", PW_TYPE_IPADDR,
822 offsetof(RADCLIENT, ipaddr), 0, NULL },
823 { "FreeRADIUS-Client-IPv6-Address", PW_TYPE_IPV6ADDR,
824 offsetof(RADCLIENT, ipaddr), 0, NULL },
826 { "FreeRADIUS-Client-Require-MA", PW_TYPE_BOOLEAN,
827 offsetof(RADCLIENT, message_authenticator), NULL, NULL },
829 { "FreeRADIUS-Client-Secret", PW_TYPE_STRING_PTR,
830 offsetof(RADCLIENT, secret), 0, "" },
831 { "FreeRADIUS-Client-Shortname", PW_TYPE_STRING_PTR,
832 offsetof(RADCLIENT, shortname), 0, "" },
833 { "FreeRADIUS-Client-NAS-Type", PW_TYPE_STRING_PTR,
834 offsetof(RADCLIENT, nastype), 0, NULL },
835 { "FreeRADIUS-Client-Virtual-Server", PW_TYPE_STRING_PTR,
836 offsetof(RADCLIENT, server), 0, NULL },
838 { NULL, -1, 0, NULL, NULL }
842 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
847 * No virtual server defined. Inherit the parent's
850 if (master->server && !c->server) {
851 c->server = strdup(master->server);
855 * If the client network isn't global (not tied to a
856 * virtual server), then ensure that this clients server
857 * is the same as the enclosing networks virtual server.
859 if (master->server &&
860 (strcmp(master->server, c->server) != 0)) {
861 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
863 buffer, sizeof(buffer)),
869 if (!client_add(clients, c)) {
870 DEBUG("- Cannot add client %s: Internal error",
872 buffer, sizeof(buffer)));
878 * Initialize the remaining fields.
881 c->lifetime = master->lifetime;
882 c->created = time(NULL);
883 c->longname = strdup(c->shortname);
885 DEBUG("- Added client %s with shared secret %s",
886 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer)),
897 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
904 if (!clients || !request) return NULL;
906 c = rad_malloc(sizeof(*c));
907 memset(c, 0, sizeof(*c));
908 c->cs = request->client->cs;
909 c->ipaddr.af = AF_UNSPEC;
911 for (i = 0; dynamic_config[i].name != NULL; i++) {
915 da = dict_attrbyname(dynamic_config[i].name);
917 DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
918 ip_ntoh(&request->packet->src_ipaddr,
919 buffer, sizeof(buffer)),
920 dynamic_config[i].name);
926 vp = pairfind(request->config_items, da->attr);
929 * Not required. Skip it.
931 if (!dynamic_config[i].dflt) continue;
933 DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",
934 ip_ntoh(&request->packet->src_ipaddr,
935 buffer, sizeof(buffer)),
936 dynamic_config[i].name);
940 switch (dynamic_config[i].type) {
942 c->ipaddr.af = AF_INET;
943 c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
947 case PW_TYPE_IPV6ADDR:
948 c->ipaddr.af = AF_INET6;
949 c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
953 case PW_TYPE_STRING_PTR:
954 p = (char **) ((char *) c + dynamic_config[i].offset);
956 *p = strdup(vp->vp_strvalue);
959 case PW_TYPE_BOOLEAN:
960 pi = (int *) ((char *) c + dynamic_config[i].offset);
961 *pi = vp->vp_integer;
969 if (c->ipaddr.af == AF_UNSPEC) {
970 DEBUG("- Cannot add client %s: No IP address was specified.",
971 ip_ntoh(&request->packet->src_ipaddr,
972 buffer, sizeof(buffer)));
977 if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
980 DEBUG("- Cannot add client %s: IP address %s do not match",
981 ip_ntoh(&request->packet->src_ipaddr,
982 buffer, sizeof(buffer)),
984 buf2, sizeof(buf2)));
988 if (!client_validate(clients, request->client, c)) {
996 * Read a client definition from the given filename.
998 RADCLIENT *client_read(const char *filename, int in_server, int flag)
1005 if (!filename) return NULL;
1007 cs = cf_file_read(filename);
1008 if (!cs) return NULL;
1010 c = client_parse(cf_section_sub_find(cs, "client"), in_server);
1012 p = strrchr(filename, FR_DIR_SEP);
1019 if (!flag) return c;
1022 * Additional validations
1024 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
1025 if (strcmp(p, buffer) != 0) {
1026 DEBUG("Invalid client definition in %s: IP address %s does not match name %s", filename, buffer, p);