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/autoconf.h>
32 #ifdef HAVE_NETINET_IN_H
33 # include <netinet/in.h>
42 #include <freeradius-devel/radiusd.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);
140 * Return a new, initialized, set of clients.
142 RADCLIENT_LIST *clients_init(void)
144 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
146 if (!clients) return NULL;
148 clients->min_prefix = 128;
155 * Sanity check a client.
157 static int client_sane(RADCLIENT *client)
159 switch (client->ipaddr.af) {
161 if (client->prefix > 32) {
166 * Zero out the subnet bits.
168 if (client->prefix < 32) {
171 mask <<= (32 - client->prefix);
172 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
177 if (client->prefix > 128) return 0;
179 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)
206 if (!clients || !client) {
210 if (client->prefix < 0) {
214 if (!client_sane(client)) return 0;
217 * Create a tree for it.
219 if (!clients->trees[client->prefix]) {
220 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
222 if (!clients->trees[client->prefix]) {
230 if (!rbtree_insert(clients->trees[client->prefix], client)) {
235 if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
237 client->number = clients->max;
239 if (clients->num) rbtree_insert(clients->num, client);
242 if (client->prefix < clients->min_prefix) {
243 clients->min_prefix = client->prefix;
251 * Find a client in the RADCLIENTS list by number.
252 * This is a support function for the SNMP code.
254 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
258 if (!clients) return NULL;
263 myclient.number = number;
265 return rbtree_finddata(clients->num, &myclient);
273 * Find a client in the RADCLIENTS list.
275 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
276 const lrad_ipaddr_t *ipaddr)
281 if (!clients || !ipaddr) return NULL;
283 switch (ipaddr->af) {
296 for (i = max_prefix; i >= clients->min_prefix; i--) {
300 myclient.ipaddr = *ipaddr;
301 client_sane(&myclient); /* clean up the ipaddress */
303 if (!clients->trees[i]) continue;
305 data = rbtree_finddata(clients->trees[i], &myclient);
316 * Old wrapper for client_find
318 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
320 return client_find(mainconfig.clients, ipaddr);
325 * Find the name of a client (prefer short name).
327 const char *client_name(const RADCLIENT_LIST *clients,
328 const lrad_ipaddr_t *ipaddr)
330 /* We don't call this unless we should know about the client. */
332 char host_ipaddr[128];
334 if ((cl = client_find(clients, ipaddr)) != NULL) {
335 if (cl->shortname && cl->shortname[0])
336 return cl->shortname;
342 * this isn't normally reachable, but if a loggable event happens just
343 * after a client list change and a HUP, then we may not know this
344 * information any more.
346 * If you see lots of these, then there's something wrong.
348 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
349 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
351 return "UNKNOWN-CLIENT";
354 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
356 return client_name(mainconfig.clients, ipaddr);
359 static const CONF_PARSER client_config[] = {
360 { "secret", PW_TYPE_STRING_PTR,
361 offsetof(RADCLIENT, secret), 0, NULL },
362 { "shortname", PW_TYPE_STRING_PTR,
363 offsetof(RADCLIENT, shortname), 0, NULL },
364 { "nastype", PW_TYPE_STRING_PTR,
365 offsetof(RADCLIENT, nastype), 0, NULL },
366 { "login", PW_TYPE_STRING_PTR,
367 offsetof(RADCLIENT, login), 0, NULL },
368 { "password", PW_TYPE_STRING_PTR,
369 offsetof(RADCLIENT, password), 0, NULL },
371 { NULL, -1, 0, NULL, NULL }
376 * Create the linked list of clients from the new configuration
377 * type. This way we don't have to change too much in the other
380 RADCLIENT_LIST *clients_parse_section(const char *filename,
381 CONF_SECTION *section)
385 char *hostnm, *prefix_ptr = NULL;
387 RADCLIENT_LIST *clients;
390 * Be forgiving. If there's already a clients, return
391 * it. Otherwise create a new one.
393 clients = cf_data_find(section, "clients");
394 if (clients) return clients;
396 clients = clients_init();
397 if (!clients) return NULL;
400 * Associate the clients structure with the section, where
401 * it will be freed once the section is freed.
403 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
404 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
405 filename, cf_section_lineno(section),
406 cf_section_name1(section));
407 clients_free(clients);
411 for (cs = cf_subsection_find_next(section, NULL, "client");
413 cs = cf_subsection_find_next(section, cs, "client")) {
414 name2 = cf_section_name2(cs);
416 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
417 filename, cf_section_lineno(cs));
421 * Check the lengths, we don't want any core dumps
424 prefix_ptr = strchr(hostnm, '/');
427 * The size is fine.. Let's create the buffer
429 c = rad_malloc(sizeof(RADCLIENT));
430 memset(c, 0, sizeof(RADCLIENT));
432 if (cf_section_parse(cs, c, client_config) < 0) {
433 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
434 filename, cf_section_lineno(cs));
443 c->prefix = atoi(prefix_ptr + 1);
444 if ((c->prefix < 0) || (c->prefix > 128)) {
445 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
446 filename, cf_section_lineno(cs), prefix_ptr + 1);
449 /* Replace '/' with '\0' */
454 * Always get the numeric representation of IP
456 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
457 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
458 filename, cf_section_lineno(cs),
459 hostnm, librad_errstr);
463 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
464 c->longname = strdup(buffer);
468 * This makes later life easier.
470 if (!c->shortname) c->shortname = strdup(c->longname);
472 if (c->prefix < 0) switch (c->ipaddr.af) {
484 * FIXME: Add the client as data via cf_data_add,
485 * for migration issues.
488 if (!client_add(clients, c)) {
489 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
490 filename, cf_section_lineno(cs), hostnm);