Delete trailing whitespace.
[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 = NULL;
111                 tree_num_max = 0;
112 #endif
113                 mainconfig.clients = NULL;
114         }
115
116         free(clients);
117 }
118
119 /*
120  *      Return a new, initialized, set of clients.
121  */
122 RADCLIENT_LIST *clients_init(void)
123 {
124         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
125
126         if (!clients) return NULL;
127
128         clients->min_prefix = 128;
129
130         return clients;
131 }
132
133
134 /*
135  *      Sanity check a client.
136  */
137 static int client_sane(RADCLIENT *client)
138 {
139         switch (client->ipaddr.af) {
140         case AF_INET:
141                 if (client->prefix > 32) {
142                         return 0;
143                 }
144
145                 /*
146                  *      Zero out the subnet bits.
147                  */
148                 if (client->prefix < 32) {
149                         uint32_t mask = ~0;
150
151                         mask <<= (32 - client->prefix);
152                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
153                 }
154                 break;
155
156         case AF_INET6:
157                 if (client->prefix > 128) return 0;
158
159                 if (client->prefix < 128) {
160                         int i;
161                         uint32_t mask, *addr;
162
163                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
164
165                         for (i = client->prefix; i < 128; i += 32) {
166                                 mask = ~0;
167                                 mask <<= ((128 - i) & 0x1f);
168                                 addr[i / 32] &= mask;
169                         }
170                 }
171                 break;
172
173         default:
174                 return 0;
175         }
176
177         return 1;
178 }
179
180
181 /*
182  *      Add a client to the tree.
183  */
184 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
185 {
186         /*
187          *      Allow clients to be NULL if mainconfig.clients is NULL.
188          */
189
190         if (!client || (!clients && (mainconfig.clients != NULL))) {
191                 return 0;
192         }
193
194         if (!clients) {
195                 clients = clients_init();
196                 if (!clients) return 0;
197                 rad_assert(mainconfig.clients == NULL);
198                 mainconfig.clients = clients;
199         }
200
201         if (client->prefix < 0) {
202                 return 0;
203         }
204
205         if (!client_sane(client)) return 0;
206
207         /*
208          *      Create a tree for it.
209          */
210         if (!clients->trees[client->prefix]) {
211                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
212                                                                client_free, 0);
213                 if (!clients->trees[client->prefix]) {
214                         return 0;
215                 }
216         }
217
218         /*
219          *      Duplicate?
220          */
221         if (!rbtree_insert(clients->trees[client->prefix], client)) {
222                 return 0;
223         }
224
225 #ifdef WITH_SNMP
226         if (!tree_num) {
227                 tree_num = rbtree_create(client_num_cmp, NULL, 0);
228         }
229
230
231         /*
232          *      Catch clients added by rlm_sql.
233          */
234         if (!client->auth) {
235                 client->auth = rad_malloc(sizeof(*client->auth));
236                 memset(client->auth, 0, sizeof(*client->auth));
237         }
238
239         if (!client->acct) {
240                 client->acct = rad_malloc(sizeof(*client->acct));
241                 memset(client->acct, 0, sizeof(*client->acct));
242         }
243
244
245         client->number = tree_num_max;
246         tree_num_max++;
247         if (tree_num) rbtree_insert(tree_num, client);
248 #endif
249
250         if (client->prefix < clients->min_prefix) {
251                 clients->min_prefix = client->prefix;
252         }
253
254         return 1;
255 }
256
257
258 /*
259  *      Find a client in the RADCLIENTS list by number.
260  *      This is a support function for the SNMP code.
261  */
262 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
263                                int number)
264 {
265 #ifdef WITH_SNMP
266         if (!clients) return NULL;
267
268         if (number >= tree_num_max) return NULL;
269
270         if (tree_num) {
271                 RADCLIENT myclient;
272
273                 myclient.number = number;
274
275                 return rbtree_finddata(tree_num, &myclient);
276         }
277 #endif
278         return NULL;
279 }
280
281
282 /*
283  *      Find a client in the RADCLIENTS list.
284  */
285 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
286                        const lrad_ipaddr_t *ipaddr)
287 {
288         int i, max_prefix;
289         RADCLIENT myclient;
290
291         if (!clients || !ipaddr) return NULL;
292
293         switch (ipaddr->af) {
294         case AF_INET:
295                 max_prefix = 32;
296                 break;
297
298         case AF_INET6:
299                 max_prefix = 128;
300                 break;
301
302         default :
303                 return NULL;
304         }
305
306         for (i = max_prefix; i >= clients->min_prefix; i--) {
307                 void *data;
308
309                 myclient.prefix = i;
310                 myclient.ipaddr = *ipaddr;
311                 client_sane(&myclient); /* clean up the ipaddress */
312
313                 if (!clients->trees[i]) continue;
314
315                 data = rbtree_finddata(clients->trees[i], &myclient);
316                 if (data) {
317                         return data;
318                 }
319         }
320
321         return NULL;
322 }
323
324
325 /*
326  *      Old wrapper for client_find
327  */
328 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
329 {
330         return client_find(mainconfig.clients, ipaddr);
331 }
332
333
334 /*
335  *      Find the name of a client (prefer short name).
336  */
337 const char *client_name(const RADCLIENT_LIST *clients,
338                         const lrad_ipaddr_t *ipaddr)
339 {
340         /* We don't call this unless we should know about the client. */
341         RADCLIENT *cl;
342         char host_ipaddr[128];
343
344         if ((cl = client_find(clients, ipaddr)) != NULL) {
345                 if (cl->shortname && cl->shortname[0])
346                         return cl->shortname;
347                 else
348                         return cl->longname;
349         }
350
351         /*
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.
355          *
356          * If you see lots of these, then there's something wrong.
357          */
358         radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
359                ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
360
361         return "UNKNOWN-CLIENT";
362 }
363
364 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
365 {
366         return client_name(mainconfig.clients, ipaddr);
367 }
368
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 },
380
381         { NULL, -1, 0, NULL, NULL }
382 };
383
384
385 /*
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
388  *      source-files.
389  */
390 RADCLIENT_LIST *clients_parse_section(const char *filename,
391                                       CONF_SECTION *section)
392 {
393         CONF_SECTION    *cs;
394         RADCLIENT       *c;
395         char            *hostnm, *prefix_ptr = NULL;
396         const char      *name2;
397         RADCLIENT_LIST  *clients;
398
399         /*
400          *      Be forgiving.  If there's already a clients, return
401          *      it.  Otherwise create a new one.
402          */
403         clients = cf_data_find(section, "clients");
404         if (clients) return clients;
405
406         clients = clients_init();
407         if (!clients) return NULL;
408
409         /*
410          *      Associate the clients structure with the section, where
411          *      it will be freed once the section is freed.
412          */
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);
418                 return NULL;
419         }
420
421         for (cs = cf_subsection_find_next(section, NULL, "client");
422              cs != NULL;
423              cs = cf_subsection_find_next(section, cs, "client")) {
424                 name2 = cf_section_name2(cs);
425                 if (!name2) {
426                         radlog(L_CONS|L_ERR, "%s[%d]: Missing client name",
427                                filename, cf_section_lineno(cs));
428                         return NULL;
429                 }
430                 /*
431                  * Check the lengths, we don't want any core dumps
432                  */
433                 hostnm = name2;
434                 prefix_ptr = strchr(hostnm, '/');
435
436                 /*
437                  * The size is fine.. Let's create the buffer
438                  */
439                 c = rad_malloc(sizeof(*c));
440                 memset(c, 0, sizeof(*c));
441
442 #ifdef WITH_SNMP
443                 c->auth = rad_malloc(sizeof(*c->auth));
444                 memset(c->auth, 0, sizeof(*c->auth));
445
446                 c->acct = rad_malloc(sizeof(*c->acct));
447                 memset(c->acct, 0, sizeof(*c->acct));
448 #endif
449
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));
453                         return NULL;
454                 }
455
456                 /*
457                  * Look for prefixes.
458                  */
459                 c->prefix = -1;
460                 if (prefix_ptr) {
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);
465                                 return NULL;
466                         }
467                         /* Replace '/' with '\0' */
468                         *prefix_ptr = '\0';
469                 }
470
471                 /*
472                  * Always get the numeric representation of IP
473                  */
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);
478                         return NULL;
479                 } else {
480                         char buffer[256];
481                         ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
482                         c->longname = strdup(buffer);
483                 }
484
485                 /*
486                  *      This makes later life easier.
487                  */
488                 if (!c->shortname) c->shortname = strdup(c->longname);
489
490                 if (c->prefix < 0) switch (c->ipaddr.af) {
491                 case AF_INET:
492                         c->prefix = 32;
493                         break;
494                 case AF_INET6:
495                         c->prefix = 128;
496                         break;
497                 default:
498                         break;
499                 }
500
501                 /*
502                  *      FIXME: Add the client as data via cf_data_add,
503                  *      for migration issues.
504                  */
505
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);
509                         client_free(c);
510                         return NULL;
511                 }
512         }
513
514         return clients;
515 }