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)) {
207 clients->num = rbtree_create(client_num_cmp, NULL, 0);
210 client->number = clients->max;
212 if (clients->num) rbtree_insert(clients->num, client);
215 if (client->prefix < clients->min_prefix) {
216 clients->min_prefix = client->prefix;
224 * Find a client in the RADCLIENTS list by number.
225 * This is a support function for the SNMP code.
227 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
231 if (!clients) return NULL;
236 myclient.number = number;
238 return rbtree_finddata(clients->num, &myclient);
246 * Find a client in the RADCLIENTS list.
248 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
249 const lrad_ipaddr_t *ipaddr)
254 if (!clients || !ipaddr) return NULL;
256 switch (ipaddr->af) {
269 for (i = max_prefix; i >= clients->min_prefix; i--) {
273 myclient.ipaddr = *ipaddr;
274 client_sane(&myclient); /* clean up the ipaddress */
276 if (!clients->trees[i]) continue;
278 data = rbtree_finddata(clients->trees[i], &myclient);
289 * Old wrapper for client_find
291 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
293 return client_find(mainconfig.clients, ipaddr);
298 * Find the name of a client (prefer short name).
300 const char *client_name(const RADCLIENT_LIST *clients,
301 const lrad_ipaddr_t *ipaddr)
303 /* We don't call this unless we should know about the client. */
305 char host_ipaddr[128];
307 if ((cl = client_find(clients, ipaddr)) != NULL) {
308 if (cl->shortname && cl->shortname[0])
309 return cl->shortname;
315 * this isn't normally reachable, but if a loggable event happens just
316 * after a client list change and a HUP, then we may not know this
317 * information any more.
319 * If you see lots of these, then there's something wrong.
321 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
322 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
324 return "UNKNOWN-CLIENT";
327 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
329 return client_name(mainconfig.clients, ipaddr);
332 static const CONF_PARSER client_config[] = {
333 { "secret", PW_TYPE_STRING_PTR,
334 offsetof(RADCLIENT, secret), 0, NULL },
335 { "shortname", PW_TYPE_STRING_PTR,
336 offsetof(RADCLIENT, shortname), 0, NULL },
337 { "nastype", PW_TYPE_STRING_PTR,
338 offsetof(RADCLIENT, nastype), 0, NULL },
339 { "login", PW_TYPE_STRING_PTR,
340 offsetof(RADCLIENT, login), 0, NULL },
341 { "password", PW_TYPE_STRING_PTR,
342 offsetof(RADCLIENT, password), 0, NULL },
344 { NULL, -1, 0, NULL, NULL }
349 * Create the linked list of clients from the new configuration
350 * type. This way we don't have to change too much in the other
353 RADCLIENT_LIST *clients_parse_section(const char *filename,
354 CONF_SECTION *section)
358 char *hostnm, *prefix_ptr = NULL;
360 RADCLIENT_LIST *clients;
363 * Be forgiving. If there's already a clients, return
364 * it. Otherwise create a new one.
366 clients = cf_data_find(section, "clients");
367 if (clients) return clients;
369 clients = clients_init();
370 if (!clients) return NULL;
373 * Associate the clients structure with the section, where
374 * it will be freed once the section is freed.
376 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
377 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
378 filename, cf_section_lineno(section),
379 cf_section_name1(section));
380 clients_free(clients);
384 for (cs = cf_subsection_find_next(section, NULL, "client");
386 cs = cf_subsection_find_next(section, cs, "client")) {
387 name2 = cf_section_name2(cs);
389 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
390 filename, cf_section_lineno(cs));
394 * Check the lengths, we don't want any core dumps
397 prefix_ptr = strchr(hostnm, '/');
400 * The size is fine.. Let's create the buffer
402 c = rad_malloc(sizeof(*c));
403 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 if (cf_section_parse(cs, c, client_config) < 0) {
414 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
415 filename, cf_section_lineno(cs));
424 c->prefix = atoi(prefix_ptr + 1);
425 if ((c->prefix < 0) || (c->prefix > 128)) {
426 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
427 filename, cf_section_lineno(cs), prefix_ptr + 1);
430 /* Replace '/' with '\0' */
435 * Always get the numeric representation of IP
437 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
438 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
439 filename, cf_section_lineno(cs),
440 hostnm, librad_errstr);
444 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
445 c->longname = strdup(buffer);
449 * This makes later life easier.
451 if (!c->shortname) c->shortname = strdup(c->longname);
453 if (c->prefix < 0) switch (c->ipaddr.af) {
465 * FIXME: Add the client as data via cf_data_add,
466 * for migration issues.
469 if (!client_add(clients, c)) {
470 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
471 filename, cf_section_lineno(cs), hostnm);