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 if (a->ipaddr.af < b->ipaddr.af) return -1;
79 if (a->ipaddr.af > b->ipaddr.af) return +1;
81 rad_assert(a->prefix == b->prefix);
83 switch (a->ipaddr.af) {
85 return memcmp(&a->ipaddr.ipaddr.ip4addr,
86 &b->ipaddr.ipaddr.ip4addr,
87 sizeof(a->ipaddr.ipaddr.ip4addr));
91 return memcmp(&a->ipaddr.ipaddr.ip6addr,
92 &b->ipaddr.ipaddr.ip6addr,
93 sizeof(a->ipaddr.ipaddr.ip6addr));
101 * Something bad happened...
103 rad_assert("Internal sanity check failed");
108 static int client_num_cmp(const void *one, const void *two)
110 const RADCLIENT *a = one;
111 const RADCLIENT *b = two;
113 return (a->number - b->number);
118 * Free a RADCLIENT list.
120 void clients_free(RADCLIENT_LIST *clients)
124 if (!clients) return;
126 for (i = 0; i <= 128; i++) {
127 if (clients->trees[i]) rbtree_free(clients->trees[i]);
128 clients->trees[i] = NULL;
131 if (clients->num) rbtree_free(clients->num);
137 * Return a new, initialized, set of clients.
139 RADCLIENT_LIST *clients_init(void)
141 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
143 if (!clients) return NULL;
145 clients->min_prefix = 128;
152 * Sanity check a client.
154 static int client_sane(RADCLIENT *client)
156 switch (client->ipaddr.af) {
158 if (client->prefix > 32) {
163 * Zero out the subnet bits.
165 if (client->prefix < 32) {
168 mask <<= (32 - client->prefix);
169 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
174 if (client->prefix > 128) return 0;
176 if (client->prefix < 128) {
178 uint32_t mask, *addr;
180 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
182 for (i = client->prefix; i < 128; i += 32) {
184 mask <<= ((128 - i) & 0x1f);
185 addr[i / 32] &= mask;
199 * Add a client to the tree.
201 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
203 if (!clients || !client) {
207 if (client->prefix < 0) {
211 if (!client_sane(client)) return 0;
214 * Create a tree for it.
216 if (!clients->trees[client->prefix]) {
217 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
219 if (!clients->trees[client->prefix]) {
227 if (!rbtree_insert(clients->trees[client->prefix], client)) {
232 if (!clients->num) rbtree_create(client_num_cmp, NULL, 0);
234 client->number = clients->max;
236 if (clients->num) rbtree_insert(clients->num, client);
239 if (client->prefix < clients->min_prefix) {
240 clients->min_prefix = client->prefix;
248 * Find a client in the RADCLIENTS list by number.
249 * This is a support function for the SNMP code.
251 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
255 if (!clients) return NULL;
260 myclient.number = number;
262 return rbtree_finddata(clients->num, &myclient);
270 * Find a client in the RADCLIENTS list.
272 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
273 const lrad_ipaddr_t *ipaddr)
278 if (!clients || !ipaddr) return NULL;
280 switch (ipaddr->af) {
293 for (i = max_prefix; i >= clients->min_prefix; i--) {
297 myclient.ipaddr = *ipaddr;
298 client_sane(&myclient); /* clean up the ipaddress */
300 if (!clients->trees[i]) continue;
302 data = rbtree_finddata(clients->trees[i], &myclient);
313 * Old wrapper for client_find
315 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
317 return client_find(mainconfig.clients, ipaddr);
322 * Find the name of a client (prefer short name).
324 const char *client_name(const RADCLIENT_LIST *clients,
325 const lrad_ipaddr_t *ipaddr)
327 /* We don't call this unless we should know about the client. */
329 char host_ipaddr[128];
331 if ((cl = client_find(clients, ipaddr)) != NULL) {
332 if (cl->shortname && cl->shortname[0])
333 return cl->shortname;
339 * this isn't normally reachable, but if a loggable event happens just
340 * after a client list change and a HUP, then we may not know this
341 * information any more.
343 * If you see lots of these, then there's something wrong.
345 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
346 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
348 return "UNKNOWN-CLIENT";
351 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
353 return client_name(mainconfig.clients, ipaddr);
356 static const CONF_PARSER client_config[] = {
357 { "secret", PW_TYPE_STRING_PTR,
358 offsetof(RADCLIENT, secret), 0, NULL },
359 { "shortname", PW_TYPE_STRING_PTR,
360 offsetof(RADCLIENT, shortname), 0, NULL },
361 { "nastype", PW_TYPE_STRING_PTR,
362 offsetof(RADCLIENT, nastype), 0, NULL },
363 { "login", PW_TYPE_STRING_PTR,
364 offsetof(RADCLIENT, login), 0, NULL },
365 { "password", PW_TYPE_STRING_PTR,
366 offsetof(RADCLIENT, password), 0, NULL },
368 { NULL, -1, 0, NULL, NULL }
373 * Create the linked list of clients from the new configuration
374 * type. This way we don't have to change too much in the other
377 RADCLIENT_LIST *clients_parse_section(const char *filename,
378 CONF_SECTION *section)
382 char *hostnm, *prefix_ptr = NULL;
384 RADCLIENT_LIST *clients;
387 * Be forgiving. If there's already a clients, return
388 * it. Otherwise create a new one.
390 clients = cf_data_find(section, "clients");
391 if (clients) return clients;
393 clients = clients_init();
394 if (!clients) return NULL;
397 * Associate the clients structure with the section, where
398 * it will be freed once the section is freed.
400 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
401 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
402 filename, cf_section_lineno(section),
403 cf_section_name1(section));
404 clients_free(clients);
408 for (cs = cf_subsection_find_next(section, NULL, "client");
410 cs = cf_subsection_find_next(section, cs, "client")) {
411 name2 = cf_section_name2(cs);
413 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
414 filename, cf_section_lineno(cs));
418 * Check the lengths, we don't want any core dumps
421 prefix_ptr = strchr(hostnm, '/');
424 * The size is fine.. Let's create the buffer
426 c = rad_malloc(sizeof(*c));
427 memset(c, 0, sizeof(*c));
430 c->auth = rad_malloc(sizeof(*c->auth));
431 memset(c->auth, 0, sizeof(*c->auth));
433 c->acct = rad_malloc(sizeof(*c->acct));
434 memset(c->acct, 0, sizeof(*c->acct));
437 if (cf_section_parse(cs, c, client_config) < 0) {
438 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
439 filename, cf_section_lineno(cs));
448 c->prefix = atoi(prefix_ptr + 1);
449 if ((c->prefix < 0) || (c->prefix > 128)) {
450 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
451 filename, cf_section_lineno(cs), prefix_ptr + 1);
454 /* Replace '/' with '\0' */
459 * Always get the numeric representation of IP
461 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
462 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
463 filename, cf_section_lineno(cs),
464 hostnm, librad_errstr);
468 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
469 c->longname = strdup(buffer);
473 * This makes later life easier.
475 if (!c->shortname) c->shortname = strdup(c->longname);
477 if (c->prefix < 0) switch (c->ipaddr.af) {
489 * FIXME: Add the client as data via cf_data_add,
490 * for migration issues.
493 if (!client_add(clients, c)) {
494 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
495 filename, cf_section_lineno(cs), hostnm);