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 The FreeRADIUS server project
21 * Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
22 * Copyright 2000 Alan DeKok <aland@ox.org>
25 static const char rcsid[] = "$Id$";
27 #include <freeradius-devel/autoconf.h>
31 #ifdef HAVE_NETINET_IN_H
32 # include <netinet/in.h>
41 #include <freeradius-devel/radiusd.h>
42 #include <freeradius-devel/conffile.h>
43 #include <freeradius-devel/rad_assert.h>
45 struct radclient_list {
47 * FIXME: One set of trees for IPv4, and another for IPv6?
49 rbtree_t *trees[129]; /* for 0..128, inclusive. */
52 rbtree_t *num; /* client numbers 0..N */
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);
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 if (a->ipaddr.af < b->ipaddr.af) return -1;
82 if (a->ipaddr.af > b->ipaddr.af) return +1;
84 rad_assert(a->prefix == b->prefix);
86 switch (a->ipaddr.af) {
88 return memcmp(&a->ipaddr.ipaddr.ip4addr,
89 &b->ipaddr.ipaddr.ip4addr,
90 sizeof(a->ipaddr.ipaddr.ip4addr));
94 return memcmp(&a->ipaddr.ipaddr.ip6addr,
95 &b->ipaddr.ipaddr.ip6addr,
96 sizeof(a->ipaddr.ipaddr.ip6addr));
104 * Something bad happened...
106 rad_assert("Internal sanity check failed");
111 static int client_num_cmp(const void *one, const void *two)
113 const RADCLIENT *a = one;
114 const RADCLIENT *b = two;
116 return (a->number - b->number);
121 * Free a RADCLIENT list.
123 void clients_free(RADCLIENT_LIST *clients)
127 if (!clients) return;
129 for (i = 0; i <= 128; i++) {
130 if (clients->trees[i]) rbtree_free(clients->trees[i]);
131 clients->trees[i] = NULL;
134 if (clients->num) rbtree_free(clients->num);
139 * Return a new, initialized, set of clients.
141 RADCLIENT_LIST *clients_init(void)
143 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
145 if (!clients) return NULL;
147 clients->min_prefix = 128;
154 * Sanity check a client.
156 static int client_sane(RADCLIENT *client)
158 switch (client->ipaddr.af) {
160 if (client->prefix > 32) {
165 * Zero out the subnet bits.
167 if (client->prefix < 32) {
170 mask <<= (32 - client->prefix);
171 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
176 if (client->prefix > 128) return 0;
178 if (client->prefix < 128) {
180 uint32_t mask, *addr;
182 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
184 for (i = client->prefix; i < 128; i += 32) {
186 mask <<= ((128 - i) & 0x1f);
187 addr[i / 32] &= mask;
201 * Add a client to the tree.
203 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
205 if (!clients || !client) {
209 if (client->prefix < 0) {
213 if (!client_sane(client)) return 0;
216 * Create a tree for it.
218 if (!clients->trees[client->prefix]) {
219 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
221 if (!clients->trees[client->prefix]) {
229 if (!rbtree_insert(clients->trees[client->prefix], client)) {
234 if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
236 client->number = clients->max;
238 if (clients->num) rbtree_insert(clients->num, client);
241 if (client->prefix < clients->min_prefix) {
242 clients->min_prefix = client->prefix;
250 * Find a client in the RADCLIENTS list by number.
251 * This is a support function for the SNMP code.
253 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
257 if (!clients) return NULL;
262 myclient.number = number;
264 return rbtree_finddata(clients->num, &myclient);
272 * Find a client in the RADCLIENTS list.
274 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
275 const lrad_ipaddr_t *ipaddr)
280 if (!clients || !ipaddr) return NULL;
282 switch (ipaddr->af) {
295 for (i = max_prefix; i >= clients->min_prefix; i--) {
299 myclient.ipaddr = *ipaddr;
300 client_sane(&myclient); /* clean up the ipaddress */
302 if (!clients->trees[i]) continue;
304 data = rbtree_finddata(clients->trees[i], &myclient);
315 * Old wrapper for client_find
317 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
319 return client_find(mainconfig.clients, ipaddr);
324 * Find the name of a client (prefer short name).
326 const char *client_name(const RADCLIENT_LIST *clients,
327 const lrad_ipaddr_t *ipaddr)
329 /* We don't call this unless we should know about the client. */
331 char host_ipaddr[128];
333 if ((cl = client_find(clients, ipaddr)) != NULL) {
334 if (cl->shortname && cl->shortname[0])
335 return cl->shortname;
341 * this isn't normally reachable, but if a loggable event happens just
342 * after a client list change and a HUP, then we may not know this
343 * information any more.
345 * If you see lots of these, then there's something wrong.
347 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
348 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
350 return "UNKNOWN-CLIENT";
353 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
355 return client_name(mainconfig.clients, ipaddr);
358 static const CONF_PARSER client_config[] = {
359 { "secret", PW_TYPE_STRING_PTR,
360 offsetof(RADCLIENT, secret), 0, NULL },
361 { "shortname", PW_TYPE_STRING_PTR,
362 offsetof(RADCLIENT, shortname), 0, NULL },
363 { "nastype", PW_TYPE_STRING_PTR,
364 offsetof(RADCLIENT, nastype), 0, NULL },
365 { "login", PW_TYPE_STRING_PTR,
366 offsetof(RADCLIENT, login), 0, NULL },
367 { "password", PW_TYPE_STRING_PTR,
368 offsetof(RADCLIENT, password), 0, NULL },
370 { NULL, -1, 0, NULL, NULL }
375 * Create the linked list of clients from the new configuration
376 * type. This way we don't have to change too much in the other
379 RADCLIENT_LIST *clients_parse_section(const char *filename,
380 CONF_SECTION *section)
384 char *hostnm, *prefix_ptr = NULL;
386 RADCLIENT_LIST *clients;
389 * Be forgiving. If there's already a clients, return
390 * it. Otherwise create a new one.
392 clients = cf_data_find(section, "clients");
393 if (clients) return clients;
395 clients = clients_init();
396 if (!clients) return NULL;
399 * Associate the clients structure with the section, where
400 * it will be freed once the section is freed.
402 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
403 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
404 filename, cf_section_lineno(section),
405 cf_section_name1(section));
406 clients_free(clients);
410 for (cs = cf_subsection_find_next(section, NULL, "client");
412 cs = cf_subsection_find_next(section, cs, "client")) {
413 name2 = cf_section_name2(cs);
415 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
416 filename, cf_section_lineno(cs));
420 * Check the lengths, we don't want any core dumps
423 prefix_ptr = strchr(hostnm, '/');
426 * The size is fine.. Let's create the buffer
428 c = rad_malloc(sizeof(RADCLIENT));
429 memset(c, 0, sizeof(RADCLIENT));
431 if (cf_section_parse(cs, c, client_config) < 0) {
432 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
433 filename, cf_section_lineno(cs));
442 c->prefix = atoi(prefix_ptr + 1);
443 if ((c->prefix < 0) || (c->prefix > 128)) {
444 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
445 filename, cf_section_lineno(cs), prefix_ptr + 1);
448 /* Replace '/' with '\0' */
453 * Always get the numeric representation of IP
455 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
456 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
457 filename, cf_section_lineno(cs),
458 hostnm, librad_errstr);
462 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
463 c->longname = strdup(buffer);
467 * This makes later life easier.
469 if (!c->shortname) c->shortname = strdup(c->longname);
471 if (c->prefix < 0) switch (c->ipaddr.af) {
483 * FIXME: Add the client as data via cf_data_add,
484 * for migration issues.
487 if (!client_add(clients, c)) {
488 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
489 filename, cf_section_lineno(cs), hostnm);