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. */
47 static rbtree_t *tree_num; /* client numbers 0..N */
48 static int tree_num_max;
52 * Callback for freeing a client.
54 void client_free(RADCLIENT *client)
56 free(client->longname);
58 free(client->shortname);
59 free(client->nastype);
61 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 return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
84 static int client_num_cmp(const void *one, const void *two)
86 const RADCLIENT *a = one;
87 const RADCLIENT *b = two;
89 return (a->number - b->number);
94 * Free a RADCLIENT list.
96 void clients_free(RADCLIENT_LIST *clients)
100 if (!clients) return;
102 for (i = 0; i <= 128; i++) {
103 if (clients->trees[i]) rbtree_free(clients->trees[i]);
104 clients->trees[i] = NULL;
107 if (clients == mainconfig.clients) {
109 if (tree_num) rbtree_free(tree_num);
112 mainconfig.clients = NULL;
119 * Return a new, initialized, set of clients.
121 RADCLIENT_LIST *clients_init(void)
123 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
125 if (!clients) return NULL;
127 clients->min_prefix = 128;
134 * Sanity check a client.
136 static int client_sane(RADCLIENT *client)
138 switch (client->ipaddr.af) {
140 if (client->prefix > 32) {
145 * Zero out the subnet bits.
147 if (client->prefix < 32) {
150 mask <<= (32 - client->prefix);
151 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
156 if (client->prefix > 128) return 0;
158 if (client->prefix < 128) {
160 uint32_t mask, *addr;
162 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
164 for (i = client->prefix; i < 128; i += 32) {
166 mask <<= ((128 - i) & 0x1f);
167 addr[i / 32] &= mask;
181 * Add a client to the tree.
183 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
186 * Allow clients to be NULL if mainconfig.clients is NULL.
189 if (!client || (!clients && (mainconfig.clients != NULL))) {
194 clients = clients_init();
195 if (!clients) return 0;
196 rad_assert(mainconfig.clients == NULL);
197 mainconfig.clients = clients;
200 if (client->prefix < 0) {
204 if (!client_sane(client)) return 0;
207 * Create a tree for it.
209 if (!clients->trees[client->prefix]) {
210 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
212 if (!clients->trees[client->prefix]) {
220 if (!rbtree_insert(clients->trees[client->prefix], client)) {
226 tree_num = rbtree_create(client_num_cmp, NULL, 0);
231 * Catch clients added by rlm_sql.
234 client->auth = rad_malloc(sizeof(*client->auth));
235 memset(client->auth, 0, sizeof(*client->auth));
239 client->acct = rad_malloc(sizeof(*client->acct));
240 memset(client->acct, 0, sizeof(*client->acct));
244 client->number = tree_num_max;
246 if (tree_num) rbtree_insert(tree_num, client);
249 if (client->prefix < clients->min_prefix) {
250 clients->min_prefix = client->prefix;
258 * Find a client in the RADCLIENTS list by number.
259 * This is a support function for the SNMP code.
261 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
265 if (!clients) return NULL;
267 if (number >= tree_num_max) return NULL;
272 myclient.number = number;
274 return rbtree_finddata(tree_num, &myclient);
282 * Find a client in the RADCLIENTS list.
284 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
285 const lrad_ipaddr_t *ipaddr)
290 if (!clients || !ipaddr) return NULL;
292 switch (ipaddr->af) {
305 for (i = max_prefix; i >= clients->min_prefix; i--) {
309 myclient.ipaddr = *ipaddr;
310 client_sane(&myclient); /* clean up the ipaddress */
312 if (!clients->trees[i]) continue;
314 data = rbtree_finddata(clients->trees[i], &myclient);
325 * Old wrapper for client_find
327 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
329 return client_find(mainconfig.clients, ipaddr);
334 * Find the name of a client (prefer short name).
336 const char *client_name(const RADCLIENT_LIST *clients,
337 const lrad_ipaddr_t *ipaddr)
339 /* We don't call this unless we should know about the client. */
341 char host_ipaddr[128];
343 if ((cl = client_find(clients, ipaddr)) != NULL) {
344 if (cl->shortname && cl->shortname[0])
345 return cl->shortname;
351 * this isn't normally reachable, but if a loggable event happens just
352 * after a client list change and a HUP, then we may not know this
353 * information any more.
355 * If you see lots of these, then there's something wrong.
357 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
358 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
360 return "UNKNOWN-CLIENT";
363 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
365 return client_name(mainconfig.clients, ipaddr);
368 static const CONF_PARSER client_config[] = {
369 { "secret", PW_TYPE_STRING_PTR,
370 offsetof(RADCLIENT, secret), 0, NULL },
371 { "shortname", PW_TYPE_STRING_PTR,
372 offsetof(RADCLIENT, shortname), 0, NULL },
373 { "nastype", PW_TYPE_STRING_PTR,
374 offsetof(RADCLIENT, nastype), 0, NULL },
375 { "login", PW_TYPE_STRING_PTR,
376 offsetof(RADCLIENT, login), 0, NULL },
377 { "password", PW_TYPE_STRING_PTR,
378 offsetof(RADCLIENT, password), 0, NULL },
380 { NULL, -1, 0, NULL, NULL }
385 * Create the linked list of clients from the new configuration
386 * type. This way we don't have to change too much in the other
389 RADCLIENT_LIST *clients_parse_section(const char *filename,
390 CONF_SECTION *section)
394 char *hostnm, *prefix_ptr = NULL;
396 RADCLIENT_LIST *clients;
399 * Be forgiving. If there's already a clients, return
400 * it. Otherwise create a new one.
402 clients = cf_data_find(section, "clients");
403 if (clients) return clients;
405 clients = clients_init();
406 if (!clients) return NULL;
409 * Associate the clients structure with the section, where
410 * it will be freed once the section is freed.
412 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
413 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
414 filename, cf_section_lineno(section),
415 cf_section_name1(section));
416 clients_free(clients);
420 for (cs = cf_subsection_find_next(section, NULL, "client");
422 cs = cf_subsection_find_next(section, cs, "client")) {
423 name2 = cf_section_name2(cs);
425 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
426 filename, cf_section_lineno(cs));
430 * Check the lengths, we don't want any core dumps
433 prefix_ptr = strchr(hostnm, '/');
436 * The size is fine.. Let's create the buffer
438 c = rad_malloc(sizeof(*c));
439 memset(c, 0, sizeof(*c));
442 c->auth = rad_malloc(sizeof(*c->auth));
443 memset(c->auth, 0, sizeof(*c->auth));
445 c->acct = rad_malloc(sizeof(*c->acct));
446 memset(c->acct, 0, sizeof(*c->acct));
449 if (cf_section_parse(cs, c, client_config) < 0) {
450 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
451 filename, cf_section_lineno(cs));
460 c->prefix = atoi(prefix_ptr + 1);
461 if ((c->prefix < 0) || (c->prefix > 128)) {
462 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
463 filename, cf_section_lineno(cs), prefix_ptr + 1);
466 /* Replace '/' with '\0' */
471 * Always get the numeric representation of IP
473 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
474 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
475 filename, cf_section_lineno(cs),
476 hostnm, librad_errstr);
480 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
481 c->longname = strdup(buffer);
485 * This makes later life easier.
487 if (!c->shortname) c->shortname = strdup(c->longname);
489 if (c->prefix < 0) switch (c->ipaddr.af) {
501 * FIXME: Add the client as data via cf_data_add,
502 * for migration issues.
505 if (!client_add(clients, c)) {
506 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
507 filename, cf_section_lineno(cs), hostnm);