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. */
44 rbtree_t *num; /* client numbers 0..N */
50 * Callback for freeing a client.
52 void client_free(RADCLIENT *client)
54 free(client->longname);
56 free(client->shortname);
57 free(client->nastype);
59 free(client->password);
71 * Callback for comparing two clients.
73 static int client_ipaddr_cmp(const void *one, const void *two)
75 const RADCLIENT *a = one;
76 const RADCLIENT *b = two;
78 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
82 static int client_num_cmp(const void *one, const void *two)
84 const RADCLIENT *a = one;
85 const RADCLIENT *b = two;
87 return (a->number - b->number);
92 * Free a RADCLIENT list.
94 void clients_free(RADCLIENT_LIST *clients)
100 for (i = 0; i <= 128; i++) {
101 if (clients->trees[i]) rbtree_free(clients->trees[i]);
102 clients->trees[i] = NULL;
105 if (clients->num) rbtree_free(clients->num);
111 * Return a new, initialized, set of clients.
113 RADCLIENT_LIST *clients_init(void)
115 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
117 if (!clients) return NULL;
119 clients->min_prefix = 128;
126 * Sanity check a client.
128 static int client_sane(RADCLIENT *client)
130 switch (client->ipaddr.af) {
132 if (client->prefix > 32) {
137 * Zero out the subnet bits.
139 if (client->prefix < 32) {
142 mask <<= (32 - client->prefix);
143 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
148 if (client->prefix > 128) return 0;
150 if (client->prefix < 128) {
152 uint32_t mask, *addr;
154 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
156 for (i = client->prefix; i < 128; i += 32) {
158 mask <<= ((128 - i) & 0x1f);
159 addr[i / 32] &= mask;
173 * Add a client to the tree.
175 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
177 if (!clients || !client) {
181 if (client->prefix < 0) {
185 if (!client_sane(client)) return 0;
188 * Create a tree for it.
190 if (!clients->trees[client->prefix]) {
191 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
193 if (!clients->trees[client->prefix]) {
201 if (!rbtree_insert(clients->trees[client->prefix], client)) {
206 if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
208 client->number = clients->max;
210 if (clients->num) rbtree_insert(clients->num, client);
213 if (client->prefix < clients->min_prefix) {
214 clients->min_prefix = client->prefix;
222 * Find a client in the RADCLIENTS list by number.
223 * This is a support function for the SNMP code.
225 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
229 if (!clients) return NULL;
234 myclient.number = number;
236 return rbtree_finddata(clients->num, &myclient);
244 * Find a client in the RADCLIENTS list.
246 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
247 const lrad_ipaddr_t *ipaddr)
252 if (!clients || !ipaddr) return NULL;
254 switch (ipaddr->af) {
267 for (i = max_prefix; i >= clients->min_prefix; i--) {
271 myclient.ipaddr = *ipaddr;
272 client_sane(&myclient); /* clean up the ipaddress */
274 if (!clients->trees[i]) continue;
276 data = rbtree_finddata(clients->trees[i], &myclient);
287 * Old wrapper for client_find
289 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
291 return client_find(mainconfig.clients, ipaddr);
296 * Find the name of a client (prefer short name).
298 const char *client_name(const RADCLIENT_LIST *clients,
299 const lrad_ipaddr_t *ipaddr)
301 /* We don't call this unless we should know about the client. */
303 char host_ipaddr[128];
305 if ((cl = client_find(clients, ipaddr)) != NULL) {
306 if (cl->shortname && cl->shortname[0])
307 return cl->shortname;
313 * this isn't normally reachable, but if a loggable event happens just
314 * after a client list change and a HUP, then we may not know this
315 * information any more.
317 * If you see lots of these, then there's something wrong.
319 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
320 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
322 return "UNKNOWN-CLIENT";
325 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
327 return client_name(mainconfig.clients, ipaddr);
330 static const CONF_PARSER client_config[] = {
331 { "secret", PW_TYPE_STRING_PTR,
332 offsetof(RADCLIENT, secret), 0, NULL },
333 { "shortname", PW_TYPE_STRING_PTR,
334 offsetof(RADCLIENT, shortname), 0, NULL },
335 { "nastype", PW_TYPE_STRING_PTR,
336 offsetof(RADCLIENT, nastype), 0, NULL },
337 { "login", PW_TYPE_STRING_PTR,
338 offsetof(RADCLIENT, login), 0, NULL },
339 { "password", PW_TYPE_STRING_PTR,
340 offsetof(RADCLIENT, password), 0, NULL },
342 { NULL, -1, 0, NULL, NULL }
347 * Create the linked list of clients from the new configuration
348 * type. This way we don't have to change too much in the other
351 RADCLIENT_LIST *clients_parse_section(const char *filename,
352 CONF_SECTION *section)
356 char *hostnm, *prefix_ptr = NULL;
358 RADCLIENT_LIST *clients;
361 * Be forgiving. If there's already a clients, return
362 * it. Otherwise create a new one.
364 clients = cf_data_find(section, "clients");
365 if (clients) return clients;
367 clients = clients_init();
368 if (!clients) return NULL;
371 * Associate the clients structure with the section, where
372 * it will be freed once the section is freed.
374 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
375 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
376 filename, cf_section_lineno(section),
377 cf_section_name1(section));
378 clients_free(clients);
382 for (cs = cf_subsection_find_next(section, NULL, "client");
384 cs = cf_subsection_find_next(section, cs, "client")) {
385 name2 = cf_section_name2(cs);
387 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
388 filename, cf_section_lineno(cs));
392 * Check the lengths, we don't want any core dumps
395 prefix_ptr = strchr(hostnm, '/');
398 * The size is fine.. Let's create the buffer
400 c = rad_malloc(sizeof(*c));
401 memset(c, 0, sizeof(*c));
404 c->auth = rad_malloc(sizeof(*c->auth));
405 memset(c->auth, 0, sizeof(*c->auth));
407 c->acct = rad_malloc(sizeof(*c->acct));
408 memset(c->acct, 0, sizeof(*c->acct));
411 if (cf_section_parse(cs, c, client_config) < 0) {
412 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
413 filename, cf_section_lineno(cs));
422 c->prefix = atoi(prefix_ptr + 1);
423 if ((c->prefix < 0) || (c->prefix > 128)) {
424 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
425 filename, cf_section_lineno(cs), prefix_ptr + 1);
428 /* Replace '/' with '\0' */
433 * Always get the numeric representation of IP
435 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
436 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
437 filename, cf_section_lineno(cs),
438 hostnm, librad_errstr);
442 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
443 c->longname = strdup(buffer);
447 * This makes later life easier.
449 if (!c->shortname) c->shortname = strdup(c->longname);
451 if (c->prefix < 0) switch (c->ipaddr.af) {
463 * FIXME: Add the client as data via cf_data_add,
464 * for migration issues.
467 if (!client_add(clients, c)) {
468 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
469 filename, cf_section_lineno(cs), hostnm);