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);
278 clients = clients; /* -Wunused */
279 number = number; /* -Wunused */
286 * Find a client in the RADCLIENTS list.
288 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
289 const lrad_ipaddr_t *ipaddr)
294 if (!clients || !ipaddr) return NULL;
296 switch (ipaddr->af) {
309 for (i = max_prefix; i >= clients->min_prefix; i--) {
313 myclient.ipaddr = *ipaddr;
314 client_sane(&myclient); /* clean up the ipaddress */
316 if (!clients->trees[i]) continue;
318 data = rbtree_finddata(clients->trees[i], &myclient);
329 * Old wrapper for client_find
331 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
333 return client_find(mainconfig.clients, ipaddr);
338 * Find the name of a client (prefer short name).
340 const char *client_name(const RADCLIENT_LIST *clients,
341 const lrad_ipaddr_t *ipaddr)
343 /* We don't call this unless we should know about the client. */
345 char host_ipaddr[128];
347 if ((cl = client_find(clients, ipaddr)) != NULL) {
348 if (cl->shortname && cl->shortname[0])
349 return cl->shortname;
355 * this isn't normally reachable, but if a loggable event happens just
356 * after a client list change and a HUP, then we may not know this
357 * information any more.
359 * If you see lots of these, then there's something wrong.
361 radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
362 ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
364 return "UNKNOWN-CLIENT";
367 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
369 return client_name(mainconfig.clients, ipaddr);
372 static struct in_addr cl_ip4addr;
373 static struct in6_addr cl_ip6addr;
375 static const CONF_PARSER client_config[] = {
376 { "ipaddr", PW_TYPE_IPADDR,
377 0, &cl_ip4addr, NULL },
378 { "ipv6addr", PW_TYPE_IPV6ADDR,
379 0, &cl_ip6addr, NULL },
380 { "netmask", PW_TYPE_INTEGER,
381 offsetof(RADCLIENT, prefix), 0, NULL },
383 { "secret", PW_TYPE_STRING_PTR,
384 offsetof(RADCLIENT, secret), 0, NULL },
385 { "shortname", PW_TYPE_STRING_PTR,
386 offsetof(RADCLIENT, shortname), 0, NULL },
387 { "nastype", PW_TYPE_STRING_PTR,
388 offsetof(RADCLIENT, nastype), 0, NULL },
389 { "login", PW_TYPE_STRING_PTR,
390 offsetof(RADCLIENT, login), 0, NULL },
391 { "password", PW_TYPE_STRING_PTR,
392 offsetof(RADCLIENT, password), 0, NULL },
393 { "server", PW_TYPE_STRING_PTR,
394 offsetof(RADCLIENT, server), 0, NULL },
396 { NULL, -1, 0, NULL, NULL }
401 * Create the linked list of clients from the new configuration
402 * type. This way we don't have to change too much in the other
405 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
410 RADCLIENT_LIST *clients;
413 * Be forgiving. If there's already a clients, return
414 * it. Otherwise create a new one.
416 clients = cf_data_find(section, "clients");
417 if (clients) return clients;
419 clients = clients_init();
420 if (!clients) return NULL;
423 * Associate the clients structure with the section, where
424 * it will be freed once the section is freed.
426 if (cf_data_add(section, "clients", clients, clients_free) < 0) {
427 cf_log_err(cf_sectiontoitem(section),
428 "Failed to associate clients with section %s",
429 cf_section_name1(section));
430 clients_free(clients);
434 for (cs = cf_subsection_find_next(section, NULL, "client");
436 cs = cf_subsection_find_next(section, cs, "client")) {
438 name2 = cf_section_name2(cs);
440 cf_log_err(cf_sectiontoitem(cs),
441 "Missing client name");
446 * The size is fine.. Let's create the buffer
448 c = rad_malloc(sizeof(*c));
449 memset(c, 0, sizeof(*c));
452 c->auth = rad_malloc(sizeof(*c->auth));
453 memset(c->auth, 0, sizeof(*c->auth));
455 c->acct = rad_malloc(sizeof(*c->acct));
456 memset(c->acct, 0, sizeof(*c->acct));
459 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
460 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
463 if (cf_section_parse(cs, c, client_config) < 0) {
465 cf_log_err(cf_sectiontoitem(cs),
466 "Error parsing client section.");
471 * Global clients can set servers to use,
472 * per-server clients cannot.
474 if ((section != mainconfig.config) && c->server) {
476 cf_log_err(cf_sectiontoitem(cs),
477 "Clients inside of an server section cannot point to another server.");
482 * No "ipaddr" or "ipv6addr", use old-style
483 * "client <ipaddr> {" syntax.
485 if (!cf_pair_find(cs, "ipaddr") &&
486 !cf_pair_find(cs, "ipv6addr")) {
489 prefix_ptr = strchr(name2, '/');
495 c->prefix = atoi(prefix_ptr + 1);
496 if ((c->prefix < 0) || (c->prefix > 128)) {
498 cf_log_err(cf_sectiontoitem(cs),
499 "Invalid Prefix value '%s' for IP.",
503 /* Replace '/' with '\0' */
508 * Always get the numeric representation of IP
510 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
512 cf_log_err(cf_sectiontoitem(cs),
513 "Failed to look up hostname %s: %s",
514 name2, librad_errstr);
518 if (prefix_ptr) *prefix_ptr = '/';
519 c->longname = strdup(name2);
521 if (!c->shortname) c->shortname = strdup(c->longname);
527 * Figure out which one to use.
529 if (cf_pair_find(cs, "ipaddr")) {
530 c->ipaddr.af = AF_INET;
531 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
533 if ((c->prefix < -1) || (c->prefix > 32)) {
535 cf_log_err(cf_sectiontoitem(cs),
536 "Netmask must be between 0 and 32");
540 } else if (cf_pair_find(cs, "ipv6addr")) {
541 c->ipaddr.af = AF_INET6;
542 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
544 if ((c->prefix < -1) || (c->prefix > 128)) {
546 cf_log_err(cf_sectiontoitem(cs),
547 "Netmask must be between 0 and 128");
551 client_free(c); /* a fairly bad error */
555 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
556 c->longname = strdup(buffer);
559 * Set the short name to the name2
561 if (!c->shortname) c->shortname = strdup(name2);
564 if (c->prefix < 0) switch (c->ipaddr.af) {
576 * FIXME: Add the client as data via cf_data_add,
577 * for migration issues.
580 if (!client_add(clients, c)) {
581 cf_log_err(cf_sectiontoitem(cs),
582 "Failed to add client %s", name2);