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);
74 * Callback for comparing two clients.
76 static int client_ipaddr_cmp(const void *one, const void *two)
78 const RADCLIENT *a = one;
79 const RADCLIENT *b = two;
81 return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
85 static int client_num_cmp(const void *one, const void *two)
87 const RADCLIENT *a = one;
88 const RADCLIENT *b = two;
90 return (a->number - b->number);
95 * Free a RADCLIENT list.
97 void clients_free(RADCLIENT_LIST *clients)
101 if (!clients) return;
103 for (i = 0; i <= 128; i++) {
104 if (clients->trees[i]) rbtree_free(clients->trees[i]);
105 clients->trees[i] = NULL;
108 if (clients == root_clients) {
110 if (tree_num) rbtree_free(tree_num);
121 * Return a new, initialized, set of clients.
123 RADCLIENT_LIST *clients_init(void)
125 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
127 if (!clients) return NULL;
129 clients->min_prefix = 128;
136 * Sanity check a client.
138 static int client_sane(RADCLIENT *client)
140 switch (client->ipaddr.af) {
142 if (client->prefix > 32) {
147 * Zero out the subnet bits.
149 if (client->prefix < 32) {
152 mask <<= (32 - client->prefix);
153 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
158 if (client->prefix > 128) return 0;
160 if (client->prefix < 128) {
162 uint32_t mask, *addr;
164 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
166 for (i = client->prefix; i < 128; i += 32) {
168 mask <<= ((128 - i) & 0x1f);
169 addr[i / 32] &= mask;
183 * Add a client to the tree.
185 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
192 * If "clients" is NULL, it means add to the global list.
196 * Initialize it, if not done already.
199 root_clients = clients_init();
200 if (!root_clients) return 0;
202 clients = root_clients;
205 if ((client->prefix < 0) || (client->prefix > 128)) {
209 if (!client_sane(client)) return 0;
212 * Create a tree for it.
214 if (!clients->trees[client->prefix]) {
215 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
216 (void *) client_free, 0);
217 if (!clients->trees[client->prefix]) {
223 * Cannot insert the same client twice.
225 if (rbtree_find(clients->trees[client->prefix], client)) {
226 radlog(L_ERR, "Failed to add duplicate client %s",
232 * Other error adding client: likely is fatal.
234 if (!rbtree_insert(clients->trees[client->prefix], client)) {
240 tree_num = rbtree_create(client_num_cmp, NULL, 0);
245 * Catch clients added by rlm_sql.
248 client->auth = rad_malloc(sizeof(*client->auth));
249 memset(client->auth, 0, sizeof(*client->auth));
253 client->acct = rad_malloc(sizeof(*client->acct));
254 memset(client->acct, 0, sizeof(*client->acct));
258 client->number = tree_num_max;
260 if (tree_num) rbtree_insert(tree_num, client);
263 if (client->prefix < clients->min_prefix) {
264 clients->min_prefix = client->prefix;
272 * Find a client in the RADCLIENTS list by number.
273 * This is a support function for the SNMP code.
275 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
279 if (!clients) clients = root_clients;
281 if (!clients) return NULL;
283 if (number >= tree_num_max) return NULL;
288 myclient.number = number;
290 return rbtree_finddata(tree_num, &myclient);
293 clients = clients; /* -Wunused */
294 number = number; /* -Wunused */
301 * Find a client in the RADCLIENTS list.
303 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
304 const fr_ipaddr_t *ipaddr)
309 if (!clients) clients = root_clients;
311 if (!clients || !ipaddr) return NULL;
313 switch (ipaddr->af) {
326 for (i = max_prefix; i >= clients->min_prefix; i--) {
330 myclient.ipaddr = *ipaddr;
331 client_sane(&myclient); /* clean up the ipaddress */
333 if (!clients->trees[i]) continue;
335 data = rbtree_finddata(clients->trees[i], &myclient);
346 * Old wrapper for client_find
348 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
350 return client_find(root_clients, ipaddr);
353 static struct in_addr cl_ip4addr;
354 static struct in6_addr cl_ip6addr;
356 static const CONF_PARSER client_config[] = {
357 { "ipaddr", PW_TYPE_IPADDR,
358 0, &cl_ip4addr, NULL },
359 { "ipv6addr", PW_TYPE_IPV6ADDR,
360 0, &cl_ip6addr, NULL },
361 { "netmask", PW_TYPE_INTEGER,
362 offsetof(RADCLIENT, prefix), 0, NULL },
364 { "require_message_authenticator", PW_TYPE_BOOLEAN,
365 offsetof(RADCLIENT, message_authenticator), 0, "no" },
367 { "secret", PW_TYPE_STRING_PTR,
368 offsetof(RADCLIENT, secret), 0, NULL },
369 { "shortname", PW_TYPE_STRING_PTR,
370 offsetof(RADCLIENT, shortname), 0, NULL },
371 { "nastype", PW_TYPE_STRING_PTR,
372 offsetof(RADCLIENT, nastype), 0, NULL },
373 { "login", PW_TYPE_STRING_PTR,
374 offsetof(RADCLIENT, login), 0, NULL },
375 { "password", PW_TYPE_STRING_PTR,
376 offsetof(RADCLIENT, password), 0, NULL },
377 { "virtual_server", PW_TYPE_STRING_PTR,
378 offsetof(RADCLIENT, server), 0, NULL },
379 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
380 offsetof(RADCLIENT, server), 0, NULL },
382 { NULL, -1, 0, NULL, NULL }
386 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
391 name2 = cf_section_name2(cs);
393 cf_log_err(cf_sectiontoitem(cs),
394 "Missing client name");
399 * The size is fine.. Let's create the buffer
401 c = rad_malloc(sizeof(*c));
402 memset(c, 0, sizeof(*c));
406 c->auth = rad_malloc(sizeof(*c->auth));
407 memset(c->auth, 0, sizeof(*c->auth));
409 c->acct = rad_malloc(sizeof(*c->acct));
410 memset(c->acct, 0, sizeof(*c->acct));
413 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
414 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
417 if (cf_section_parse(cs, c, client_config) < 0) {
419 cf_log_err(cf_sectiontoitem(cs),
420 "Error parsing client section.");
425 * Global clients can set servers to use,
426 * per-server clients cannot.
428 if (in_server && c->server) {
430 cf_log_err(cf_sectiontoitem(cs),
431 "Clients inside of an server section cannot point to a server.");
436 * No "ipaddr" or "ipv6addr", use old-style
437 * "client <ipaddr> {" syntax.
439 if (!cf_pair_find(cs, "ipaddr") &&
440 !cf_pair_find(cs, "ipv6addr")) {
443 prefix_ptr = strchr(name2, '/');
449 c->prefix = atoi(prefix_ptr + 1);
450 if ((c->prefix < 0) || (c->prefix > 128)) {
452 cf_log_err(cf_sectiontoitem(cs),
453 "Invalid Prefix value '%s' for IP.",
457 /* Replace '/' with '\0' */
462 * Always get the numeric representation of IP
464 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
466 cf_log_err(cf_sectiontoitem(cs),
467 "Failed to look up hostname %s: %s",
468 name2, librad_errstr);
472 if (prefix_ptr) *prefix_ptr = '/';
473 c->longname = strdup(name2);
475 if (!c->shortname) c->shortname = strdup(c->longname);
481 * Figure out which one to use.
483 if (cf_pair_find(cs, "ipaddr")) {
484 c->ipaddr.af = AF_INET;
485 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
487 if ((c->prefix < -1) || (c->prefix > 32)) {
489 cf_log_err(cf_sectiontoitem(cs),
490 "Netmask must be between 0 and 32");
494 } else if (cf_pair_find(cs, "ipv6addr")) {
495 c->ipaddr.af = AF_INET6;
496 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
498 if ((c->prefix < -1) || (c->prefix > 128)) {
500 cf_log_err(cf_sectiontoitem(cs),
501 "Netmask must be between 0 and 128");
505 cf_log_err(cf_sectiontoitem(cs),
506 "No IP address defined for the client");
511 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
512 c->longname = strdup(buffer);
515 * Set the short name to the name2
517 if (!c->shortname) c->shortname = strdup(name2);
520 if (c->prefix < 0) switch (c->ipaddr.af) {
531 if (!c->secret || !*c->secret) {
533 cf_log_err(cf_sectiontoitem(cs),
534 "secret must be at least 1 character long");
543 * Create the linked list of clients from the new configuration
544 * type. This way we don't have to change too much in the other
547 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
549 int global = FALSE, in_server = FALSE;
552 RADCLIENT_LIST *clients;
555 * Be forgiving. If there's already a clients, return
556 * it. Otherwise create a new one.
558 clients = cf_data_find(section, "clients");
559 if (clients) return clients;
561 clients = clients_init();
562 if (!clients) return NULL;
564 if (cf_top_section(section) == section) global = TRUE;
566 if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
569 * Associate the clients structure with the section, where
570 * it will be freed once the section is freed.
572 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
573 cf_log_err(cf_sectiontoitem(section),
574 "Failed to associate clients with section %s",
575 cf_section_name1(section));
576 clients_free(clients);
580 for (cs = cf_subsection_find_next(section, NULL, "client");
582 cs = cf_subsection_find_next(section, cs, "client")) {
583 c = client_parse(cs, in_server);
589 * FIXME: Add the client as data via cf_data_add,
590 * for migration issues.
593 if (!client_add(clients, c)) {
594 cf_log_err(cf_sectiontoitem(cs),
595 "Failed to add client %s",
596 cf_section_name2(cs));
603 * Replace the global list of clients with the new one.
604 * The old one is still referenced from the original
605 * configuration, and will be freed when that is freed.
608 root_clients = clients;