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/radius_snmp.h>
30 #include <freeradius-devel/rad_assert.h>
37 struct radclient_list {
39 * FIXME: One set of trees for IPv4, and another for IPv6?
41 rbtree_t *trees[129]; /* for 0..128, inclusive. */
47 static rbtree_t *tree_num; /* client numbers 0..N */
48 static int tree_num_max;
50 static RADCLIENT_LIST *root_clients;
53 * Callback for freeing a client.
55 void client_free(RADCLIENT *client)
57 free(client->longname);
59 free(client->shortname);
60 free(client->nastype);
62 free(client->password);
75 * Callback for comparing two clients.
77 static int client_ipaddr_cmp(const void *one, const void *two)
79 const RADCLIENT *a = one;
80 const RADCLIENT *b = two;
82 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
86 static int client_num_cmp(const void *one, const void *two)
88 const RADCLIENT *a = one;
89 const RADCLIENT *b = two;
91 return (a->number - b->number);
96 * Free a RADCLIENT list.
98 void clients_free(RADCLIENT_LIST *clients)
102 if (!clients) return;
104 for (i = 0; i <= 128; i++) {
105 if (clients->trees[i]) rbtree_free(clients->trees[i]);
106 clients->trees[i] = NULL;
109 if (clients == root_clients) {
111 if (tree_num) rbtree_free(tree_num);
122 * Return a new, initialized, set of clients.
124 RADCLIENT_LIST *clients_init(void)
126 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
128 if (!clients) return NULL;
130 clients->min_prefix = 128;
137 * Sanity check a client.
139 static int client_sane(RADCLIENT *client)
141 switch (client->ipaddr.af) {
143 if (client->prefix > 32) {
148 * Zero out the subnet bits.
150 if (client->prefix == 0) {
151 memset(&client->ipaddr.ipaddr.ip4addr, 0,
152 sizeof(client->ipaddr.ipaddr.ip4addr));
154 } else if (client->prefix < 32) {
157 mask <<= (32 - client->prefix);
158 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
163 if (client->prefix > 128) return 0;
165 if (client->prefix == 0) {
166 memset(&client->ipaddr.ipaddr.ip6addr, 0,
167 sizeof(client->ipaddr.ipaddr.ip6addr));
169 } else if (client->prefix < 128) {
171 uint32_t mask, *addr;
173 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
175 for (i = client->prefix; i < 128; i += 32) {
177 mask <<= ((128 - i) & 0x1f);
178 addr[i / 32] &= mask;
192 * Add a client to the tree.
194 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
201 * If "clients" is NULL, it means add to the global list.
205 * Initialize it, if not done already.
208 root_clients = clients_init();
209 if (!root_clients) return 0;
211 clients = root_clients;
214 if ((client->prefix < 0) || (client->prefix > 128)) {
218 if (!client_sane(client)) return 0;
221 * Create a tree for it.
223 if (!clients->trees[client->prefix]) {
224 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
225 (void *) client_free, 0);
226 if (!clients->trees[client->prefix]) {
232 * Cannot insert the same client twice.
234 if (rbtree_find(clients->trees[client->prefix], client)) {
235 radlog(L_ERR, "Failed to add duplicate client %s",
241 * Other error adding client: likely is fatal.
243 if (!rbtree_insert(clients->trees[client->prefix], client)) {
249 tree_num = rbtree_create(client_num_cmp, NULL, 0);
254 * Catch clients added by rlm_sql.
257 client->auth = rad_malloc(sizeof(*client->auth));
258 memset(client->auth, 0, sizeof(*client->auth));
262 client->acct = rad_malloc(sizeof(*client->acct));
263 memset(client->acct, 0, sizeof(*client->acct));
267 client->number = tree_num_max;
269 if (tree_num) rbtree_insert(tree_num, client);
272 if (client->prefix < clients->min_prefix) {
273 clients->min_prefix = client->prefix;
281 * Find a client in the RADCLIENTS list by number.
282 * This is a support function for the SNMP code.
284 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
288 if (!clients) clients = root_clients;
290 if (!clients) return NULL;
292 if (number >= tree_num_max) return NULL;
297 myclient.number = number;
299 return rbtree_finddata(tree_num, &myclient);
302 clients = clients; /* -Wunused */
303 number = number; /* -Wunused */
310 * Find a client in the RADCLIENTS list.
312 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
313 const fr_ipaddr_t *ipaddr)
318 if (!clients) clients = root_clients;
320 if (!clients || !ipaddr) return NULL;
322 switch (ipaddr->af) {
335 for (i = max_prefix; i >= clients->min_prefix; i--) {
339 myclient.ipaddr = *ipaddr;
340 client_sane(&myclient); /* clean up the ipaddress */
342 if (!clients->trees[i]) continue;
344 data = rbtree_finddata(clients->trees[i], &myclient);
355 * Old wrapper for client_find
357 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
359 return client_find(root_clients, ipaddr);
362 static struct in_addr cl_ip4addr;
363 static struct in6_addr cl_ip6addr;
365 static const CONF_PARSER client_config[] = {
366 { "ipaddr", PW_TYPE_IPADDR,
367 0, &cl_ip4addr, NULL },
368 { "ipv6addr", PW_TYPE_IPV6ADDR,
369 0, &cl_ip6addr, NULL },
370 { "netmask", PW_TYPE_INTEGER,
371 offsetof(RADCLIENT, prefix), 0, NULL },
373 { "require_message_authenticator", PW_TYPE_BOOLEAN,
374 offsetof(RADCLIENT, message_authenticator), 0, "no" },
376 { "secret", PW_TYPE_STRING_PTR,
377 offsetof(RADCLIENT, secret), 0, NULL },
378 { "shortname", PW_TYPE_STRING_PTR,
379 offsetof(RADCLIENT, shortname), 0, NULL },
380 { "nastype", PW_TYPE_STRING_PTR,
381 offsetof(RADCLIENT, nastype), 0, NULL },
382 { "login", PW_TYPE_STRING_PTR,
383 offsetof(RADCLIENT, login), 0, NULL },
384 { "password", PW_TYPE_STRING_PTR,
385 offsetof(RADCLIENT, password), 0, NULL },
386 { "virtual_server", PW_TYPE_STRING_PTR,
387 offsetof(RADCLIENT, server), 0, NULL },
388 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
389 offsetof(RADCLIENT, server), 0, NULL },
391 { NULL, -1, 0, NULL, NULL }
395 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
400 name2 = cf_section_name2(cs);
402 cf_log_err(cf_sectiontoitem(cs),
403 "Missing client name");
408 * The size is fine.. Let's create the buffer
410 c = rad_malloc(sizeof(*c));
411 memset(c, 0, sizeof(*c));
415 c->auth = rad_malloc(sizeof(*c->auth));
416 memset(c->auth, 0, sizeof(*c->auth));
418 c->acct = rad_malloc(sizeof(*c->acct));
419 memset(c->acct, 0, sizeof(*c->acct));
422 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
423 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
426 if (cf_section_parse(cs, c, client_config) < 0) {
428 cf_log_err(cf_sectiontoitem(cs),
429 "Error parsing client section.");
434 * Global clients can set servers to use,
435 * per-server clients cannot.
437 if (in_server && c->server) {
439 cf_log_err(cf_sectiontoitem(cs),
440 "Clients inside of an server section cannot point to a server.");
445 * No "ipaddr" or "ipv6addr", use old-style
446 * "client <ipaddr> {" syntax.
448 if (!cf_pair_find(cs, "ipaddr") &&
449 !cf_pair_find(cs, "ipv6addr")) {
452 prefix_ptr = strchr(name2, '/');
458 c->prefix = atoi(prefix_ptr + 1);
459 if ((c->prefix < 0) || (c->prefix > 128)) {
461 cf_log_err(cf_sectiontoitem(cs),
462 "Invalid Prefix value '%s' for IP.",
466 /* Replace '/' with '\0' */
471 * Always get the numeric representation of IP
473 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
475 cf_log_err(cf_sectiontoitem(cs),
476 "Failed to look up hostname %s: %s",
477 name2, librad_errstr);
481 if (prefix_ptr) *prefix_ptr = '/';
482 c->longname = strdup(name2);
484 if (!c->shortname) c->shortname = strdup(c->longname);
490 * Figure out which one to use.
492 if (cf_pair_find(cs, "ipaddr")) {
493 c->ipaddr.af = AF_INET;
494 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
496 if ((c->prefix < -1) || (c->prefix > 32)) {
498 cf_log_err(cf_sectiontoitem(cs),
499 "Netmask must be between 0 and 32");
503 } else if (cf_pair_find(cs, "ipv6addr")) {
504 c->ipaddr.af = AF_INET6;
505 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
507 if ((c->prefix < -1) || (c->prefix > 128)) {
509 cf_log_err(cf_sectiontoitem(cs),
510 "Netmask must be between 0 and 128");
514 cf_log_err(cf_sectiontoitem(cs),
515 "No IP address defined for the client");
520 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
521 c->longname = strdup(buffer);
524 * Set the short name to the name2
526 if (!c->shortname) c->shortname = strdup(name2);
529 if (c->prefix < 0) switch (c->ipaddr.af) {
540 if (!c->secret || !*c->secret) {
542 const char *value = NULL;
543 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
545 if (cp) value = cf_pair_value(cp);
548 * Secrets aren't needed for DHCP.
550 if (value && (strcmp(value, "yes") == 0)) return c;
554 cf_log_err(cf_sectiontoitem(cs),
555 "secret must be at least 1 character long");
564 * Create the linked list of clients from the new configuration
565 * type. This way we don't have to change too much in the other
568 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
570 int global = FALSE, in_server = FALSE;
573 RADCLIENT_LIST *clients;
576 * Be forgiving. If there's already a clients, return
577 * it. Otherwise create a new one.
579 clients = cf_data_find(section, "clients");
580 if (clients) return clients;
582 clients = clients_init();
583 if (!clients) return NULL;
585 if (cf_top_section(section) == section) global = TRUE;
587 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
590 * Associate the clients structure with the section, where
591 * it will be freed once the section is freed.
593 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
594 cf_log_err(cf_sectiontoitem(section),
595 "Failed to associate clients with section %s",
596 cf_section_name1(section));
597 clients_free(clients);
601 for (cs = cf_subsection_find_next(section, NULL, "client");
603 cs = cf_subsection_find_next(section, cs, "client")) {
604 c = client_parse(cs, in_server);
610 * FIXME: Add the client as data via cf_data_add,
611 * for migration issues.
614 if (!client_add(clients, c)) {
615 cf_log_err(cf_sectiontoitem(cs),
616 "Failed to add client %s",
617 cf_section_name2(cs));
624 * Replace the global list of clients with the new one.
625 * The old one is still referenced from the original
626 * configuration, and will be freed when that is freed.
629 root_clients = clients;