If free'ing mainconfig.clients, set it to NULL.
[freeradius.git] / src / main / client.c
1 /*
2  * client.c     Read clients into memory.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
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>
23  */
24
25 #include <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/radius_snmp.h>
30 #include <freeradius-devel/rad_assert.h>
31
32 #include <sys/stat.h>
33
34 #include <ctype.h>
35 #include <fcntl.h>
36
37 struct radclient_list {
38         /*
39          *      FIXME: One set of trees for IPv4, and another for IPv6?
40          */
41         rbtree_t        *trees[129]; /* for 0..128, inclusive. */
42         int             min_prefix;
43 };
44
45
46 #ifdef WITH_SNMP
47 static rbtree_t         *tree_num;      /* client numbers 0..N */
48 static int              tree_num_max;
49 #endif
50
51 /*
52  *      Callback for freeing a client.
53  */
54 void client_free(RADCLIENT *client)
55 {
56         free(client->longname);
57         free(client->secret);
58         free(client->shortname);
59         free(client->nastype);
60         free(client->login);
61         free(client->password);
62
63 #ifdef WITH_SNMP
64         free(client->auth);
65         free(client->acct);
66 #endif
67
68         free(client);
69 }
70
71
72 /*
73  *      Callback for comparing two clients.
74  */
75 static int client_ipaddr_cmp(const void *one, const void *two)
76 {
77         const RADCLIENT *a = one;
78         const RADCLIENT *b = two;
79
80         return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
81 }
82
83 #ifdef WITH_SNMP
84 static int client_num_cmp(const void *one, const void *two)
85 {
86         const RADCLIENT *a = one;
87         const RADCLIENT *b = two;
88
89         return (a->number - b->number);
90 }
91 #endif
92
93 /*
94  *      Free a RADCLIENT list.
95  */
96 void clients_free(RADCLIENT_LIST *clients)
97 {
98         int i;
99
100         if (!clients) return;
101
102         for (i = 0; i <= 128; i++) {
103                 if (clients->trees[i]) rbtree_free(clients->trees[i]);
104                 clients->trees[i] = NULL;
105         }
106
107         if (clients == mainconfig.clients) {
108 #ifdef WITH_SNMP
109                 if (tree_num) rbtree_free(tree_num);
110                 tree_num_max = 0;
111 #endif
112                 mainconfig.clients = NULL;
113         }
114
115         free(clients);
116 }
117
118 /*
119  *      Return a new, initialized, set of clients.
120  */
121 RADCLIENT_LIST *clients_init(void)
122 {
123         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
124
125         if (!clients) return NULL;
126
127         clients->min_prefix = 128;
128
129         return clients;
130 }
131
132
133 /*
134  *      Sanity check a client.
135  */
136 static int client_sane(RADCLIENT *client)
137 {
138         switch (client->ipaddr.af) {
139         case AF_INET:
140                 if (client->prefix > 32) {
141                         return 0;
142                 }
143
144                 /*
145                  *      Zero out the subnet bits.
146                  */
147                 if (client->prefix < 32) {
148                         uint32_t mask = ~0;
149
150                         mask <<= (32 - client->prefix);
151                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
152                 }
153                 break;
154
155         case AF_INET6:
156                 if (client->prefix > 128) return 0;
157
158                 if (client->prefix < 128) {
159                         int i;
160                         uint32_t mask, *addr;
161
162                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
163
164                         for (i = client->prefix; i < 128; i += 32) {
165                                 mask = ~0;
166                                 mask <<= ((128 - i) & 0x1f);
167                                 addr[i / 32] &= mask;
168                         }
169                 }
170                 break;
171
172         default:
173                 return 0;
174         }
175
176         return 1;
177 }
178
179
180 /*
181  *      Add a client to the tree.
182  */
183 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
184 {
185         /*
186          *      Allow clients to be NULL if mainconfig.clients is NULL.
187          */
188          
189         if (!client || (!clients && (mainconfig.clients != NULL))) {
190                 return 0;
191         }
192         
193         if (!clients) {
194                 clients = clients_init();
195                 if (!clients) return 0;
196                 rad_assert(mainconfig.clients == NULL);
197                 mainconfig.clients = clients;
198         }
199
200         if (client->prefix < 0) {
201                 return 0;
202         }
203
204         if (!client_sane(client)) return 0;
205
206         /*
207          *      Create a tree for it.
208          */
209         if (!clients->trees[client->prefix]) {
210                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
211                                                                client_free, 0);
212                 if (!clients->trees[client->prefix]) {
213                         return 0;
214                 }
215         }
216
217         /*
218          *      Duplicate?
219          */
220         if (!rbtree_insert(clients->trees[client->prefix], client)) {
221                 return 0;
222         }
223
224 #ifdef WITH_SNMP
225         if (!tree_num) {
226                 tree_num = rbtree_create(client_num_cmp, NULL, 0);
227         }
228
229
230         /*
231          *      Catch clients added by rlm_sql.
232          */
233         if (!client->auth) {
234                 client->auth = rad_malloc(sizeof(*client->auth));
235                 memset(client->auth, 0, sizeof(*client->auth));
236         }
237
238         if (!client->acct) {
239                 client->acct = rad_malloc(sizeof(*client->acct));
240                 memset(client->acct, 0, sizeof(*client->acct));
241         }
242
243
244         client->number = tree_num_max;
245         tree_num_max++;
246         if (tree_num) rbtree_insert(tree_num, client);
247 #endif
248
249         if (client->prefix < clients->min_prefix) {
250                 clients->min_prefix = client->prefix;
251         }
252
253         return 1;
254 }
255
256
257 /*
258  *      Find a client in the RADCLIENTS list by number.
259  *      This is a support function for the SNMP code.
260  */
261 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
262                                int number)
263 {
264 #ifdef WITH_SNMP
265         if (!clients) return NULL;
266
267         if (number >= tree_num_max) return NULL;
268
269         if (tree_num) {
270                 RADCLIENT myclient;
271                 
272                 myclient.number = number;
273                 
274                 return rbtree_finddata(tree_num, &myclient);
275         }
276 #endif
277         return NULL;
278 }
279
280
281 /*
282  *      Find a client in the RADCLIENTS list.
283  */
284 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
285                        const lrad_ipaddr_t *ipaddr)
286 {
287         int i, max_prefix;
288         RADCLIENT myclient;
289
290         if (!clients || !ipaddr) return NULL;
291
292         switch (ipaddr->af) {
293         case AF_INET:
294                 max_prefix = 32;
295                 break;
296
297         case AF_INET6:
298                 max_prefix = 128;
299                 break;
300
301         default :
302                 return NULL;
303         }
304
305         for (i = max_prefix; i >= clients->min_prefix; i--) {
306                 void *data;
307
308                 myclient.prefix = i;
309                 myclient.ipaddr = *ipaddr;
310                 client_sane(&myclient); /* clean up the ipaddress */
311
312                 if (!clients->trees[i]) continue;
313                 
314                 data = rbtree_finddata(clients->trees[i], &myclient);
315                 if (data) {
316                         return data;
317                 }
318         }
319
320         return NULL;
321 }
322
323
324 /*
325  *      Old wrapper for client_find
326  */
327 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
328 {
329         return client_find(mainconfig.clients, ipaddr);
330 }
331
332
333 /*
334  *      Find the name of a client (prefer short name).
335  */
336 const char *client_name(const RADCLIENT_LIST *clients,
337                         const lrad_ipaddr_t *ipaddr)
338 {
339         /* We don't call this unless we should know about the client. */
340         RADCLIENT *cl;
341         char host_ipaddr[128];
342
343         if ((cl = client_find(clients, ipaddr)) != NULL) {
344                 if (cl->shortname && cl->shortname[0])
345                         return cl->shortname;
346                 else
347                         return cl->longname;
348         }
349
350         /*
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.
354          *
355          * If you see lots of these, then there's something wrong.
356          */
357         radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
358                ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
359
360         return "UNKNOWN-CLIENT";
361 }
362
363 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
364 {
365         return client_name(mainconfig.clients, ipaddr);
366 }
367
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 },
379
380         { NULL, -1, 0, NULL, NULL }
381 };
382
383
384 /*
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
387  *      source-files.
388  */
389 RADCLIENT_LIST *clients_parse_section(const char *filename,
390                                       CONF_SECTION *section)
391 {
392         CONF_SECTION    *cs;
393         RADCLIENT       *c;
394         char            *hostnm, *prefix_ptr = NULL;
395         const char      *name2;
396         RADCLIENT_LIST  *clients;
397
398         /*
399          *      Be forgiving.  If there's already a clients, return
400          *      it.  Otherwise create a new one.
401          */
402         clients = cf_data_find(section, "clients");
403         if (clients) return clients;
404
405         clients = clients_init();
406         if (!clients) return NULL;
407
408         /*
409          *      Associate the clients structure with the section, where
410          *      it will be freed once the section is freed.
411          */
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);
417                 return NULL;
418         }
419
420         for (cs = cf_subsection_find_next(section, NULL, "client");
421              cs != NULL;
422              cs = cf_subsection_find_next(section, cs, "client")) {
423                 name2 = cf_section_name2(cs);
424                 if (!name2) {
425                         radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
426                                filename, cf_section_lineno(cs));
427                         return NULL;
428                 }
429                 /*
430                  * Check the lengths, we don't want any core dumps
431                  */
432                 hostnm = name2;
433                 prefix_ptr = strchr(hostnm, '/');
434
435                 /*
436                  * The size is fine.. Let's create the buffer
437                  */
438                 c = rad_malloc(sizeof(*c));
439                 memset(c, 0, sizeof(*c));
440
441 #ifdef WITH_SNMP
442                 c->auth = rad_malloc(sizeof(*c->auth));
443                 memset(c->auth, 0, sizeof(*c->auth));
444
445                 c->acct = rad_malloc(sizeof(*c->acct));
446                 memset(c->acct, 0, sizeof(*c->acct));
447 #endif
448
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));
452                         return NULL;
453                 }
454
455                 /*
456                  * Look for prefixes.
457                  */
458                 c->prefix = -1;
459                 if (prefix_ptr) {
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);
464                                 return NULL;
465                         }
466                         /* Replace '/' with '\0' */
467                         *prefix_ptr = '\0';
468                 }
469
470                 /*
471                  * Always get the numeric representation of IP
472                  */
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);
477                         return NULL;
478                 } else {
479                         char buffer[256];
480                         ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
481                         c->longname = strdup(buffer);
482                 }
483
484                 /*
485                  *      This makes later life easier.
486                  */
487                 if (!c->shortname) c->shortname = strdup(c->longname);
488
489                 if (c->prefix < 0) switch (c->ipaddr.af) {
490                 case AF_INET:
491                         c->prefix = 32;
492                         break;
493                 case AF_INET6:
494                         c->prefix = 128;
495                         break;
496                 default:
497                         break;
498                 }
499
500                 /*
501                  *      FIXME: Add the client as data via cf_data_add,
502                  *      for migration issues.
503                  */
504
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);
508                         client_free(c);
509                         return NULL;
510                 }
511         }
512
513         return clients;
514 }