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/rad_assert.h>
44 struct radclient_list {
46 * FIXME: One set of trees for IPv4, and another for IPv6?
48 rbtree_t *trees[129]; /* for 0..128, inclusive. */
51 rbtree_t *num; /* client numbers 0..N */
57 * Callback for freeing a client.
59 void client_free(RADCLIENT *client)
61 free(client->longname);
63 free(client->shortname);
64 free(client->nastype);
66 free(client->password);
73 * Callback for comparing two clients.
75 static int client_ipaddr_cmp(const void *one, const void *two)
77 const RADCLIENT *a = one;
78 const RADCLIENT *b = two;
80 if (a->ipaddr.af < b->ipaddr.af) return -1;
81 if (a->ipaddr.af > b->ipaddr.af) return +1;
83 rad_assert(a->prefix == b->prefix);
85 switch (a->ipaddr.af) {
87 return memcmp(&a->ipaddr.ipaddr.ip4addr,
88 &b->ipaddr.ipaddr.ip4addr,
89 sizeof(a->ipaddr.ipaddr.ip4addr));
93 return memcmp(&a->ipaddr.ipaddr.ip6addr,
94 &b->ipaddr.ipaddr.ip6addr,
95 sizeof(a->ipaddr.ipaddr.ip6addr));
103 * Something bad happened...
105 rad_assert("Internal sanity check failed");
110 static int client_num_cmp(const void *one, const void *two)
112 const RADCLIENT *a = one;
113 const RADCLIENT *b = two;
115 return (a->number - b->number);
120 * Free a RADCLIENT list.
122 void clients_free(RADCLIENT_LIST *clients)
126 if (!clients) return;
128 for (i = 0; i <= 128; i++) {
129 if (clients->trees[i]) rbtree_free(clients->trees[i]);
130 clients->trees[i] = NULL;
133 if (clients->num) rbtree_free(clients->num);
138 * Return a new, initialized, set of clients.
140 RADCLIENT_LIST *clients_init(void)
142 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
144 if (!clients) return NULL;
146 clients->min_prefix = 128;
153 * Sanity check a client.
155 static int client_sane(RADCLIENT *client)
157 switch (client->ipaddr.af) {
159 if (client->prefix > 32) {
164 * Zero out the subnet bits.
166 if (client->prefix < 32) {
169 mask <<= (32 - client->prefix);
170 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
175 if (client->prefix > 128) return 0;
177 if (client->prefix < 128) {
179 uint32_t mask, *addr;
181 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
183 for (i = client->prefix; i < 128; i += 32) {
185 mask <<= ((128 - i) & 0x1f);
186 addr[i / 32] &= mask;
200 * Add a client to the tree.
202 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
204 if (!clients || !client) {
208 if (client->prefix < 0) {
212 if (!client_sane(client)) return 0;
215 * Create a tree for it.
217 if (!clients->trees[client->prefix]) {
218 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
220 if (!clients->trees[client->prefix]) {
228 if (!rbtree_insert(clients->trees[client->prefix], client)) {
233 if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
235 client->number = clients->max;
237 if (clients->num) rbtree_insert(clients->num, client);
240 if (client->prefix < clients->min_prefix) {
241 clients->min_prefix = client->prefix;
249 * Find a client in the RADCLIENTS list by number.
250 * This is a support function for the SNMP code.
252 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
256 if (!clients) return NULL;
261 myclient.number = number;
263 return rbtree_finddata(clients->num, &myclient);
271 * Find a client in the RADCLIENTS list.
273 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
274 const lrad_ipaddr_t *ipaddr)
279 if (!clients || !ipaddr) return NULL;
281 switch (ipaddr->af) {
294 for (i = max_prefix; i >= clients->min_prefix; i--) {
298 myclient.ipaddr = *ipaddr;
299 client_sane(&myclient); /* clean up the ipaddress */
301 if (!clients->trees[i]) continue;
303 data = rbtree_finddata(clients->trees[i], &myclient);
314 * Old wrapper for client_find
316 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
318 return client_find(mainconfig.clients, ipaddr);
323 * Find the name of a client (prefer short name).
325 const char *client_name(const RADCLIENT_LIST *clients,
326 const lrad_ipaddr_t *ipaddr)
328 /* We don't call this unless we should know about the client. */
330 char host_ipaddr[128];
332 if ((cl = client_find(clients, ipaddr)) != NULL) {
333 if (cl->shortname && cl->shortname[0])
334 return cl->shortname;
340 * this isn't normally reachable, but if a loggable event happens just
341 * after a client list change and a HUP, then we may not know this
342 * information any more.
344 * If you see lots of these, then there's something wrong.
346 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
347 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
349 return "UNKNOWN-CLIENT";
352 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
354 return client_name(mainconfig.clients, ipaddr);
357 static const CONF_PARSER client_config[] = {
358 { "secret", PW_TYPE_STRING_PTR,
359 offsetof(RADCLIENT, secret), 0, NULL },
360 { "shortname", PW_TYPE_STRING_PTR,
361 offsetof(RADCLIENT, shortname), 0, NULL },
362 { "nastype", PW_TYPE_STRING_PTR,
363 offsetof(RADCLIENT, nastype), 0, NULL },
364 { "login", PW_TYPE_STRING_PTR,
365 offsetof(RADCLIENT, login), 0, NULL },
366 { "password", PW_TYPE_STRING_PTR,
367 offsetof(RADCLIENT, password), 0, NULL },
369 { NULL, -1, 0, NULL, NULL }
374 * Create the linked list of clients from the new configuration
375 * type. This way we don't have to change too much in the other
378 RADCLIENT_LIST *clients_parse_section(const char *filename,
379 CONF_SECTION *section)
383 char *hostnm, *prefix_ptr = NULL;
385 RADCLIENT_LIST *clients;
388 * Be forgiving. If there's already a clients, return
389 * it. Otherwise create a new one.
391 clients = cf_data_find(section, "clients");
392 if (clients) return clients;
394 clients = clients_init();
395 if (!clients) return NULL;
398 * Associate the clients structure with the section, where
399 * it will be freed once the section is freed.
401 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
402 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
403 filename, cf_section_lineno(section),
404 cf_section_name1(section));
405 clients_free(clients);
409 for (cs = cf_subsection_find_next(section, NULL, "client");
411 cs = cf_subsection_find_next(section, cs, "client")) {
412 name2 = cf_section_name2(cs);
414 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
415 filename, cf_section_lineno(cs));
419 * Check the lengths, we don't want any core dumps
422 prefix_ptr = strchr(hostnm, '/');
425 * The size is fine.. Let's create the buffer
427 c = rad_malloc(sizeof(RADCLIENT));
428 memset(c, 0, sizeof(RADCLIENT));
430 if (cf_section_parse(cs, c, client_config) < 0) {
431 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
432 filename, cf_section_lineno(cs));
441 c->prefix = atoi(prefix_ptr + 1);
442 if ((c->prefix < 0) || (c->prefix > 128)) {
443 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
444 filename, cf_section_lineno(cs), prefix_ptr + 1);
447 /* Replace '/' with '\0' */
452 * Always get the numeric representation of IP
454 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
455 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
456 filename, cf_section_lineno(cs),
457 hostnm, librad_errstr);
461 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
462 c->longname = strdup(buffer);
466 * This makes later life easier.
468 if (!c->shortname) c->shortname = strdup(c->longname);
470 if (c->prefix < 0) switch (c->ipaddr.af) {
482 * FIXME: Add the client as data via cf_data_add,
483 * for migration issues.
486 if (!client_add(clients, c)) {
487 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
488 filename, cf_section_lineno(cs), hostnm);