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);
113 mainconfig.clients = NULL;
120 * Return a new, initialized, set of clients.
122 RADCLIENT_LIST *clients_init(void)
124 RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
126 if (!clients) return NULL;
128 clients->min_prefix = 128;
135 * Sanity check a client.
137 static int client_sane(RADCLIENT *client)
139 switch (client->ipaddr.af) {
141 if (client->prefix > 32) {
146 * Zero out the subnet bits.
148 if (client->prefix < 32) {
151 mask <<= (32 - client->prefix);
152 client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
157 if (client->prefix > 128) return 0;
159 if (client->prefix < 128) {
161 uint32_t mask, *addr;
163 addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
165 for (i = client->prefix; i < 128; i += 32) {
167 mask <<= ((128 - i) & 0x1f);
168 addr[i / 32] &= mask;
182 * Add a client to the tree.
184 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
187 * Allow clients to be NULL if mainconfig.clients is NULL.
190 if (!client || (!clients && (mainconfig.clients != NULL))) {
195 clients = clients_init();
196 if (!clients) return 0;
197 rad_assert(mainconfig.clients == NULL);
198 mainconfig.clients = clients;
201 if (client->prefix < 0) {
205 if (!client_sane(client)) return 0;
208 * Create a tree for it.
210 if (!clients->trees[client->prefix]) {
211 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
213 if (!clients->trees[client->prefix]) {
221 if (!rbtree_insert(clients->trees[client->prefix], client)) {
227 tree_num = rbtree_create(client_num_cmp, NULL, 0);
232 * Catch clients added by rlm_sql.
235 client->auth = rad_malloc(sizeof(*client->auth));
236 memset(client->auth, 0, sizeof(*client->auth));
240 client->acct = rad_malloc(sizeof(*client->acct));
241 memset(client->acct, 0, sizeof(*client->acct));
245 client->number = tree_num_max;
247 if (tree_num) rbtree_insert(tree_num, client);
250 if (client->prefix < clients->min_prefix) {
251 clients->min_prefix = client->prefix;
259 * Find a client in the RADCLIENTS list by number.
260 * This is a support function for the SNMP code.
262 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
266 if (!clients) return NULL;
268 if (number >= tree_num_max) return NULL;
273 myclient.number = number;
275 return rbtree_finddata(tree_num, &myclient);
283 * Find a client in the RADCLIENTS list.
285 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
286 const lrad_ipaddr_t *ipaddr)
291 if (!clients || !ipaddr) return NULL;
293 switch (ipaddr->af) {
306 for (i = max_prefix; i >= clients->min_prefix; i--) {
310 myclient.ipaddr = *ipaddr;
311 client_sane(&myclient); /* clean up the ipaddress */
313 if (!clients->trees[i]) continue;
315 data = rbtree_finddata(clients->trees[i], &myclient);
326 * Old wrapper for client_find
328 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
330 return client_find(mainconfig.clients, ipaddr);
335 * Find the name of a client (prefer short name).
337 const char *client_name(const RADCLIENT_LIST *clients,
338 const lrad_ipaddr_t *ipaddr)
340 /* We don't call this unless we should know about the client. */
342 char host_ipaddr[128];
344 if ((cl = client_find(clients, ipaddr)) != NULL) {
345 if (cl->shortname && cl->shortname[0])
346 return cl->shortname;
352 * this isn't normally reachable, but if a loggable event happens just
353 * after a client list change and a HUP, then we may not know this
354 * information any more.
356 * If you see lots of these, then there's something wrong.
358 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
359 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
361 return "UNKNOWN-CLIENT";
364 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
366 return client_name(mainconfig.clients, ipaddr);
369 static const CONF_PARSER client_config[] = {
370 { "secret", PW_TYPE_STRING_PTR,
371 offsetof(RADCLIENT, secret), 0, NULL },
372 { "shortname", PW_TYPE_STRING_PTR,
373 offsetof(RADCLIENT, shortname), 0, NULL },
374 { "nastype", PW_TYPE_STRING_PTR,
375 offsetof(RADCLIENT, nastype), 0, NULL },
376 { "login", PW_TYPE_STRING_PTR,
377 offsetof(RADCLIENT, login), 0, NULL },
378 { "password", PW_TYPE_STRING_PTR,
379 offsetof(RADCLIENT, password), 0, NULL },
381 { NULL, -1, 0, NULL, NULL }
386 * Create the linked list of clients from the new configuration
387 * type. This way we don't have to change too much in the other
390 RADCLIENT_LIST *clients_parse_section(const char *filename,
391 CONF_SECTION *section)
395 char *hostnm, *prefix_ptr = NULL;
397 RADCLIENT_LIST *clients;
400 * Be forgiving. If there's already a clients, return
401 * it. Otherwise create a new one.
403 clients = cf_data_find(section, "clients");
404 if (clients) return clients;
406 clients = clients_init();
407 if (!clients) return NULL;
410 * Associate the clients structure with the section, where
411 * it will be freed once the section is freed.
413 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
414 radlog(L_ERR, "%s[%d]: Failed to associate clients with section %s",
415 filename, cf_section_lineno(section),
416 cf_section_name1(section));
417 clients_free(clients);
421 for (cs = cf_subsection_find_next(section, NULL, "client");
423 cs = cf_subsection_find_next(section, cs, "client")) {
424 name2 = cf_section_name2(cs);
426 radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
427 filename, cf_section_lineno(cs));
431 * Check the lengths, we don't want any core dumps
434 prefix_ptr = strchr(hostnm, '/');
437 * The size is fine.. Let's create the buffer
439 c = rad_malloc(sizeof(*c));
440 memset(c, 0, sizeof(*c));
443 c->auth = rad_malloc(sizeof(*c->auth));
444 memset(c->auth, 0, sizeof(*c->auth));
446 c->acct = rad_malloc(sizeof(*c->acct));
447 memset(c->acct, 0, sizeof(*c->acct));
450 if (cf_section_parse(cs, c, client_config) < 0) {
451 radlog(L_CONS|L_ERR, "%s[%d]: Error parsing client section.",
452 filename, cf_section_lineno(cs));
461 c->prefix = atoi(prefix_ptr + 1);
462 if ((c->prefix < 0) || (c->prefix > 128)) {
463 radlog(L_ERR, "%s[%d]: Invalid Prefix value '%s' for IP.",
464 filename, cf_section_lineno(cs), prefix_ptr + 1);
467 /* Replace '/' with '\0' */
472 * Always get the numeric representation of IP
474 if (ip_hton(hostnm, AF_UNSPEC, &c->ipaddr) < 0) {
475 radlog(L_CONS|L_ERR, "%s[%d]: Failed to look up hostname %s: %s",
476 filename, cf_section_lineno(cs),
477 hostnm, librad_errstr);
481 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
482 c->longname = strdup(buffer);
486 * This makes later life easier.
488 if (!c->shortname) c->shortname = strdup(c->longname);
490 if (c->prefix < 0) switch (c->ipaddr.af) {
502 * FIXME: Add the client as data via cf_data_add,
503 * for migration issues.
506 if (!client_add(clients, c)) {
507 radlog(L_CONS|L_ERR, "%s[%d]: Failed to add client %s",
508 filename, cf_section_lineno(cs), hostnm);