Update clients from root_clients, if possible
[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 static RADCLIENT_LIST   *root_clients;
51
52 /*
53  *      Callback for freeing a client.
54  */
55 void client_free(RADCLIENT *client)
56 {
57         free(client->longname);
58         free(client->secret);
59         free(client->shortname);
60         free(client->nastype);
61         free(client->login);
62         free(client->password);
63
64 #ifdef WITH_SNMP
65         free(client->auth);
66         free(client->acct);
67 #endif
68
69         free(client);
70 }
71
72
73 /*
74  *      Callback for comparing two clients.
75  */
76 static int client_ipaddr_cmp(const void *one, const void *two)
77 {
78         const RADCLIENT *a = one;
79         const RADCLIENT *b = two;
80
81         return lrad_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
82 }
83
84 #ifdef WITH_SNMP
85 static int client_num_cmp(const void *one, const void *two)
86 {
87         const RADCLIENT *a = one;
88         const RADCLIENT *b = two;
89
90         return (a->number - b->number);
91 }
92 #endif
93
94 /*
95  *      Free a RADCLIENT list.
96  */
97 void clients_free(RADCLIENT_LIST *clients)
98 {
99         int i;
100
101         if (!clients) return;
102
103         for (i = 0; i <= 128; i++) {
104                 if (clients->trees[i]) rbtree_free(clients->trees[i]);
105                 clients->trees[i] = NULL;
106         }
107
108         if (clients == root_clients) {
109 #ifdef WITH_SNMP
110                 if (tree_num) rbtree_free(tree_num);
111                 tree_num = NULL;
112                 tree_num_max = 0;
113 #endif
114                 root_clients = NULL;
115         }
116
117         free(clients);
118 }
119
120 /*
121  *      Return a new, initialized, set of clients.
122  */
123 RADCLIENT_LIST *clients_init(void)
124 {
125         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
126
127         if (!clients) return NULL;
128
129         clients->min_prefix = 128;
130
131         return clients;
132 }
133
134
135 /*
136  *      Sanity check a client.
137  */
138 static int client_sane(RADCLIENT *client)
139 {
140         switch (client->ipaddr.af) {
141         case AF_INET:
142                 if (client->prefix > 32) {
143                         return 0;
144                 }
145
146                 /*
147                  *      Zero out the subnet bits.
148                  */
149                 if (client->prefix < 32) {
150                         uint32_t mask = ~0;
151
152                         mask <<= (32 - client->prefix);
153                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
154                 }
155                 break;
156
157         case AF_INET6:
158                 if (client->prefix > 128) return 0;
159
160                 if (client->prefix < 128) {
161                         int i;
162                         uint32_t mask, *addr;
163
164                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
165
166                         for (i = client->prefix; i < 128; i += 32) {
167                                 mask = ~0;
168                                 mask <<= ((128 - i) & 0x1f);
169                                 addr[i / 32] &= mask;
170                         }
171                 }
172                 break;
173
174         default:
175                 return 0;
176         }
177
178         return 1;
179 }
180
181
182 /*
183  *      Add a client to the tree.
184  */
185 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
186 {
187         if (!client) {
188                 return 0;
189         }
190
191         /*
192          *      If "clients" is NULL, it means add to the global list.
193          */
194         if (!clients) {
195                 /*
196                  *      Initialize it, if not done already.
197                  */
198                 if (!root_clients) {
199                         root_clients = clients_init();
200                         if (!root_clients) return 0;
201                         rad_assert(root_clients == NULL);
202                 }
203                 clients = root_clients;
204         }
205
206         if ((client->prefix < 0) || (client->prefix > 128)) {
207                 return 0;
208         }
209
210         if (!client_sane(client)) return 0;
211
212         /*
213          *      Create a tree for it.
214          */
215         if (!clients->trees[client->prefix]) {
216                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
217                                                                client_free, 0);
218                 if (!clients->trees[client->prefix]) {
219                         return 0;
220                 }
221         }
222
223         /*
224          *      Duplicate?
225          */
226         if (!rbtree_insert(clients->trees[client->prefix], client)) {
227                 return 0;
228         }
229
230 #ifdef WITH_SNMP
231         if (!tree_num) {
232                 tree_num = rbtree_create(client_num_cmp, NULL, 0);
233         }
234
235
236         /*
237          *      Catch clients added by rlm_sql.
238          */
239         if (!client->auth) {
240                 client->auth = rad_malloc(sizeof(*client->auth));
241                 memset(client->auth, 0, sizeof(*client->auth));
242         }
243
244         if (!client->acct) {
245                 client->acct = rad_malloc(sizeof(*client->acct));
246                 memset(client->acct, 0, sizeof(*client->acct));
247         }
248
249
250         client->number = tree_num_max;
251         tree_num_max++;
252         if (tree_num) rbtree_insert(tree_num, client);
253 #endif
254
255         if (client->prefix < clients->min_prefix) {
256                 clients->min_prefix = client->prefix;
257         }
258
259         return 1;
260 }
261
262
263 /*
264  *      Find a client in the RADCLIENTS list by number.
265  *      This is a support function for the SNMP code.
266  */
267 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
268                                int number)
269 {
270 #ifdef WITH_SNMP
271         if (!clients) clients = root_clients;
272
273         if (!clients) return NULL;
274
275         if (number >= tree_num_max) return NULL;
276
277         if (tree_num) {
278                 RADCLIENT myclient;
279
280                 myclient.number = number;
281
282                 return rbtree_finddata(tree_num, &myclient);
283         }
284 #else
285         clients = clients;      /* -Wunused */
286         number = number;        /* -Wunused */
287 #endif
288         return NULL;
289 }
290
291
292 /*
293  *      Find a client in the RADCLIENTS list.
294  */
295 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
296                        const lrad_ipaddr_t *ipaddr)
297 {
298         int i, max_prefix;
299         RADCLIENT myclient;
300
301         if (!clients) clients = root_clients;
302
303         if (!clients || !ipaddr) return NULL;
304
305         switch (ipaddr->af) {
306         case AF_INET:
307                 max_prefix = 32;
308                 break;
309
310         case AF_INET6:
311                 max_prefix = 128;
312                 break;
313
314         default :
315                 return NULL;
316         }
317
318         for (i = max_prefix; i >= clients->min_prefix; i--) {
319                 void *data;
320
321                 myclient.prefix = i;
322                 myclient.ipaddr = *ipaddr;
323                 client_sane(&myclient); /* clean up the ipaddress */
324
325                 if (!clients->trees[i]) continue;
326
327                 data = rbtree_finddata(clients->trees[i], &myclient);
328                 if (data) {
329                         return data;
330                 }
331         }
332
333         return NULL;
334 }
335
336
337 /*
338  *      Old wrapper for client_find
339  */
340 RADCLIENT *client_find_old(const lrad_ipaddr_t *ipaddr)
341 {
342         return client_find(root_clients, ipaddr);
343 }
344
345
346 /*
347  *      Find the name of a client (prefer short name).
348  */
349 const char *client_name(const RADCLIENT_LIST *clients,
350                         const lrad_ipaddr_t *ipaddr)
351 {
352         /* We don't call this unless we should know about the client. */
353         RADCLIENT *cl;
354         char host_ipaddr[128];
355
356         if ((cl = client_find(clients, ipaddr)) != NULL) {
357                 if (cl->shortname && cl->shortname[0])
358                         return cl->shortname;
359                 else
360                         return cl->longname;
361         }
362
363         /*
364          * this isn't normally reachable, but if a loggable event happens just
365          * after a client list change and a HUP, then we may not know this
366          * information any more.
367          *
368          * If you see lots of these, then there's something wrong.
369          */
370         radlog(L_ERR, "Trying to look up name of unknown client %s.\n",
371                ip_ntoh(ipaddr, host_ipaddr, sizeof(host_ipaddr)));
372
373         return "UNKNOWN-CLIENT";
374 }
375
376 const char *client_name_old(const lrad_ipaddr_t *ipaddr)
377 {
378         return client_name(root_clients, ipaddr);
379 }
380
381 static struct in_addr cl_ip4addr;
382 static struct in6_addr cl_ip6addr;
383
384 static const CONF_PARSER client_config[] = {
385         { "ipaddr",  PW_TYPE_IPADDR,
386           0, &cl_ip4addr,  NULL },
387         { "ipv6addr",  PW_TYPE_IPV6ADDR,
388           0, &cl_ip6addr, NULL },
389         { "netmask",  PW_TYPE_INTEGER,
390           offsetof(RADCLIENT, prefix), 0, NULL },
391
392         { "secret",  PW_TYPE_STRING_PTR,
393           offsetof(RADCLIENT, secret), 0, NULL },
394         { "shortname",  PW_TYPE_STRING_PTR,
395           offsetof(RADCLIENT, shortname), 0, NULL },
396         { "nastype",  PW_TYPE_STRING_PTR,
397           offsetof(RADCLIENT, nastype), 0, NULL },
398         { "login",  PW_TYPE_STRING_PTR,
399           offsetof(RADCLIENT, login), 0, NULL },
400         { "password",  PW_TYPE_STRING_PTR,
401           offsetof(RADCLIENT, password), 0, NULL },
402         { "virtual_server",  PW_TYPE_STRING_PTR,
403           offsetof(RADCLIENT, server), 0, NULL },
404         { "server",  PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
405           offsetof(RADCLIENT, server), 0, NULL },
406
407         { NULL, -1, 0, NULL, NULL }
408 };
409
410
411 /*
412  *      Create the linked list of clients from the new configuration
413  *      type.  This way we don't have to change too much in the other
414  *      source-files.
415  */
416 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
417 {
418         CONF_SECTION    *cs, *parentcs;
419         RADCLIENT       *c;
420         const char      *name2;
421         RADCLIENT_LIST  *clients;
422
423         /*
424          *      Be forgiving.  If there's already a clients, return
425          *      it.  Otherwise create a new one.
426          */
427         clients = cf_data_find(section, "clients");
428         if (clients) return clients;
429
430         clients = clients_init();
431         if (!clients) return NULL;
432
433         parentcs = cf_top_section(section);
434
435         /*
436          *      Associate the clients structure with the section, where
437          *      it will be freed once the section is freed.
438          */
439         if (cf_data_add(section, "clients", clients, clients_free) < 0) {
440                 cf_log_err(cf_sectiontoitem(section),
441                            "Failed to associate clients with section %s",
442                        cf_section_name1(section));
443                 clients_free(clients);
444                 return NULL;
445         }
446
447         for (cs = cf_subsection_find_next(section, NULL, "client");
448              cs != NULL;
449              cs = cf_subsection_find_next(section, cs, "client")) {
450
451                 name2 = cf_section_name2(cs);
452                 if (!name2) {
453                         cf_log_err(cf_sectiontoitem(cs),
454                                    "Missing client name");
455                         return NULL;
456                 }
457
458                 /*
459                  * The size is fine.. Let's create the buffer
460                  */
461                 c = rad_malloc(sizeof(*c));
462                 memset(c, 0, sizeof(*c));
463                 c->cs = cs;
464
465 #ifdef WITH_SNMP
466                 c->auth = rad_malloc(sizeof(*c->auth));
467                 memset(c->auth, 0, sizeof(*c->auth));
468
469                 c->acct = rad_malloc(sizeof(*c->acct));
470                 memset(c->acct, 0, sizeof(*c->acct));
471 #endif
472
473                 memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
474                 memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
475                 c->prefix = -1;
476
477                 if (cf_section_parse(cs, c, client_config) < 0) {
478                         client_free(c);
479                         cf_log_err(cf_sectiontoitem(cs),
480                                    "Error parsing client section.");
481                         return NULL;
482                 }
483
484                 /*
485                  *      Global clients can set servers to use,
486                  *      per-server clients cannot.
487                  */
488                 if ((section != parentcs) && c->server) {
489                         client_free(c);
490                         cf_log_err(cf_sectiontoitem(cs),
491                                    "Clients inside of an server section cannot point to another server.");
492                         return NULL;
493                 }
494                 
495                 /*
496                  *      No "ipaddr" or "ipv6addr", use old-style
497                  *      "client <ipaddr> {" syntax.
498                  */
499                 if (!cf_pair_find(cs, "ipaddr") &&
500                     !cf_pair_find(cs, "ipv6addr")) {
501                         char *prefix_ptr;
502
503                         prefix_ptr = strchr(name2, '/');
504
505                         /*
506                          *      Look for prefixes.
507                          */
508                         if (prefix_ptr) {
509                                 c->prefix = atoi(prefix_ptr + 1);
510                                 if ((c->prefix < 0) || (c->prefix > 128)) {
511                                         client_free(c);
512                                         cf_log_err(cf_sectiontoitem(cs),
513                                                    "Invalid Prefix value '%s' for IP.",
514                                                    prefix_ptr + 1);
515                                         return NULL;
516                                 }
517                                 /* Replace '/' with '\0' */
518                                 *prefix_ptr = '\0';
519                         }
520                         
521                         /*
522                          *      Always get the numeric representation of IP
523                          */
524                         if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
525                                 client_free(c);
526                                 cf_log_err(cf_sectiontoitem(cs),
527                                            "Failed to look up hostname %s: %s",
528                                            name2, librad_errstr);
529                                 return NULL;
530                         }
531
532                         if (prefix_ptr) *prefix_ptr = '/';
533                         c->longname = strdup(name2);
534
535                         if (!c->shortname) c->shortname = strdup(c->longname);
536
537                 } else {
538                         char buffer[1024];
539
540                         /*
541                          *      Figure out which one to use.
542                          */
543                         if (cf_pair_find(cs, "ipaddr")) {
544                                 c->ipaddr.af = AF_INET;
545                                 c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
546
547                                 if ((c->prefix < -1) || (c->prefix > 32)) {
548                                         client_free(c);
549                                         cf_log_err(cf_sectiontoitem(cs),
550                                                    "Netmask must be between 0 and 32");
551                                         return NULL;
552                                 }
553                                 
554                         } else if (cf_pair_find(cs, "ipv6addr")) {
555                                 c->ipaddr.af = AF_INET6;
556                                 c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
557                                 
558                                 if ((c->prefix < -1) || (c->prefix > 128)) {
559                                         client_free(c);
560                                         cf_log_err(cf_sectiontoitem(cs),
561                                                    "Netmask must be between 0 and 128");
562                                         return NULL;
563                                 }
564                         } else {
565                                 client_free(c); /* a fairly bad error */
566                                 return NULL;
567                         }
568
569                         ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
570                         c->longname = strdup(buffer);
571
572                         /*
573                          *      Set the short name to the name2
574                          */
575                         if (!c->shortname) c->shortname = strdup(name2);
576                 }
577
578                 if (c->prefix < 0) switch (c->ipaddr.af) {
579                 case AF_INET:
580                         c->prefix = 32;
581                         break;
582                 case AF_INET6:
583                         c->prefix = 128;
584                         break;
585                 default:
586                         break;
587                 }
588
589                 /*
590                  *      FIXME: Add the client as data via cf_data_add,
591                  *      for migration issues.
592                  */
593
594                 if (!client_add(clients, c)) {
595                         cf_log_err(cf_sectiontoitem(cs),
596                                    "Failed to add client %s", name2);
597                         client_free(c);
598                         return NULL;
599                 }
600         }
601
602         /*
603          *      Replace the global list of clients with the new one.
604          *      The old one is still referenced from the original
605          *      configuration, and will be freed when that is freed.
606          */
607         if (section == parentcs) {
608                 root_clients = clients;
609         }
610
611         return clients;
612 }