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]) {
225 if (!rbtree_insert(clients->trees[client->prefix], client)) {
231 tree_num = rbtree_create(client_num_cmp, NULL, 0);
236 * Catch clients added by rlm_sql.
239 client->auth = rad_malloc(sizeof(*client->auth));
240 memset(client->auth, 0, sizeof(*client->auth));
244 client->acct = rad_malloc(sizeof(*client->acct));
245 memset(client->acct, 0, sizeof(*client->acct));
249 client->number = tree_num_max;
251 if (tree_num) rbtree_insert(tree_num, client);
254 if (client->prefix < clients->min_prefix) {
255 clients->min_prefix = client->prefix;
263 * Find a client in the RADCLIENTS list by number.
264 * This is a support function for the SNMP code.
266 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
270 if (!clients) clients = root_clients;
272 if (!clients) return NULL;
274 if (number >= tree_num_max) return NULL;
279 myclient.number = number;
281 return rbtree_finddata(tree_num, &myclient);
284 clients = clients; /* -Wunused */
285 number = number; /* -Wunused */
292 * Find a client in the RADCLIENTS list.
294 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
295 const fr_ipaddr_t *ipaddr)
300 if (!clients) clients = root_clients;
302 if (!clients || !ipaddr) return NULL;
304 switch (ipaddr->af) {
317 for (i = max_prefix; i >= clients->min_prefix; i--) {
321 myclient.ipaddr = *ipaddr;
322 client_sane(&myclient); /* clean up the ipaddress */
324 if (!clients->trees[i]) continue;
326 data = rbtree_finddata(clients->trees[i], &myclient);
337 * Old wrapper for client_find
339 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
341 return client_find(root_clients, ipaddr);
346 * Find the name of a client (prefer short name).
348 const char *client_name(const RADCLIENT_LIST *clients,
349 const fr_ipaddr_t *ipaddr)
351 /* We don't call this unless we should know about the client. */
353 char host_ipaddr[128];
355 if ((cl = client_find(clients, ipaddr)) != NULL) {
356 if (cl->shortname && cl->shortname[0])
357 return cl->shortname;
363 * this isn't normally reachable, but if a loggable event happens just
364 * after a client list change and a HUP, then we may not know this
365 * information any more.
367 * If you see lots of these, then there's something wrong.
369 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
370 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
372 return "UNKNOWN-CLIENT";
375 const char *client_name_old(const fr_ipaddr_t *ipaddr)
377 return client_name(root_clients, ipaddr);
380 static struct in_addr cl_ip4addr;
381 static struct in6_addr cl_ip6addr;
383 static const CONF_PARSER client_config[] = {
384 { "ipaddr", PW_TYPE_IPADDR,
385 0, &cl_ip4addr, NULL },
386 { "ipv6addr", PW_TYPE_IPV6ADDR,
387 0, &cl_ip6addr, NULL },
388 { "netmask", PW_TYPE_INTEGER,
389 offsetof(RADCLIENT, prefix), 0, NULL },
391 { "require_message_authenticator", PW_TYPE_BOOLEAN,
392 offsetof(RADCLIENT, message_authenticator), 0, "no" },
394 { "secret", PW_TYPE_STRING_PTR,
395 offsetof(RADCLIENT, secret), 0, NULL },
396 { "shortname", PW_TYPE_STRING_PTR,
397 offsetof(RADCLIENT, shortname), 0, NULL },
398 { "nastype", PW_TYPE_STRING_PTR,
399 offsetof(RADCLIENT, nastype), 0, NULL },
400 { "login", PW_TYPE_STRING_PTR,
401 offsetof(RADCLIENT, login), 0, NULL },
402 { "password", PW_TYPE_STRING_PTR,
403 offsetof(RADCLIENT, password), 0, NULL },
404 { "virtual_server", PW_TYPE_STRING_PTR,
405 offsetof(RADCLIENT, server), 0, NULL },
406 { "server", PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
407 offsetof(RADCLIENT, server), 0, NULL },
409 { NULL, -1, 0, NULL, NULL }
413 static RADCLIENT *client_parse(CONF_SECTION *cs, int global)
418 name2 = cf_section_name2(cs);
420 cf_log_err(cf_sectiontoitem(cs),
421 "Missing client name");
426 * The size is fine.. Let's create the buffer
428 c = rad_malloc(sizeof(*c));
429 memset(c, 0, sizeof(*c));
433 c->auth = rad_malloc(sizeof(*c->auth));
434 memset(c->auth, 0, sizeof(*c->auth));
436 c->acct = rad_malloc(sizeof(*c->acct));
437 memset(c->acct, 0, sizeof(*c->acct));
440 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
441 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
444 if (cf_section_parse(cs, c, client_config) < 0) {
446 cf_log_err(cf_sectiontoitem(cs),
447 "Error parsing client section.");
452 * Global clients can set servers to use,
453 * per-server clients cannot.
455 if (!global && c->server) {
457 cf_log_err(cf_sectiontoitem(cs),
458 "Clients inside of an server section cannot point to a server.");
463 * No "ipaddr" or "ipv6addr", use old-style
464 * "client <ipaddr> {" syntax.
466 if (!cf_pair_find(cs, "ipaddr") &&
467 !cf_pair_find(cs, "ipv6addr")) {
470 prefix_ptr = strchr(name2, '/');
476 c->prefix = atoi(prefix_ptr + 1);
477 if ((c->prefix < 0) || (c->prefix > 128)) {
479 cf_log_err(cf_sectiontoitem(cs),
480 "Invalid Prefix value '%s' for IP.",
484 /* Replace '/' with '\0' */
489 * Always get the numeric representation of IP
491 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
493 cf_log_err(cf_sectiontoitem(cs),
494 "Failed to look up hostname %s: %s",
495 name2, librad_errstr);
499 if (prefix_ptr) *prefix_ptr = '/';
500 c->longname = strdup(name2);
502 if (!c->shortname) c->shortname = strdup(c->longname);
508 * Figure out which one to use.
510 if (cf_pair_find(cs, "ipaddr")) {
511 c->ipaddr.af = AF_INET;
512 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
514 if ((c->prefix < -1) || (c->prefix > 32)) {
516 cf_log_err(cf_sectiontoitem(cs),
517 "Netmask must be between 0 and 32");
521 } else if (cf_pair_find(cs, "ipv6addr")) {
522 c->ipaddr.af = AF_INET6;
523 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
525 if ((c->prefix < -1) || (c->prefix > 128)) {
527 cf_log_err(cf_sectiontoitem(cs),
528 "Netmask must be between 0 and 128");
532 cf_log_err(cf_sectiontoitem(cs),
533 "No IP address defined for the client");
538 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
539 c->longname = strdup(buffer);
542 * Set the short name to the name2
544 if (!c->shortname) c->shortname = strdup(name2);
547 if (c->prefix < 0) switch (c->ipaddr.af) {
562 * Create the linked list of clients from the new configuration
563 * type. This way we don't have to change too much in the other
566 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
571 RADCLIENT_LIST *clients;
574 * Be forgiving. If there's already a clients, return
575 * it. Otherwise create a new one.
577 clients = cf_data_find(section, "clients");
578 if (clients) return clients;
580 clients = clients_init();
581 if (!clients) return NULL;
583 if (cf_top_section(section) == section) global = TRUE;
586 * Associate the clients structure with the section, where
587 * it will be freed once the section is freed.
589 if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
590 cf_log_err(cf_sectiontoitem(section),
591 "Failed to associate clients with section %s",
592 cf_section_name1(section));
593 clients_free(clients);
597 for (cs = cf_subsection_find_next(section, NULL, "client");
599 cs = cf_subsection_find_next(section, cs, "client")) {
600 c = client_parse(cs, global);
606 * FIXME: Add the client as data via cf_data_add,
607 * for migration issues.
610 if (!client_add(clients, c)) {
611 cf_log_err(cf_sectiontoitem(cs),
612 "Failed to add client %s",
613 cf_section_name2(cs));
620 * Replace the global list of clients with the new one.
621 * The old one is still referenced from the original
622 * configuration, and will be freed when that is freed.
625 root_clients = clients;