7df88d35f9c0b60b200ecafe00c65942e3780032
[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/rad_assert.h>
30
31 #include <sys/stat.h>
32
33 #include <ctype.h>
34 #include <fcntl.h>
35
36 #ifdef WITH_DYNAMIC_CLIENTS
37 #ifdef HAVE_DIRENT_H
38 #include <dirent.h>
39 #endif
40 #endif
41
42 struct radclient_list {
43         /*
44          *      FIXME: One set of trees for IPv4, and another for IPv6?
45          */
46         rbtree_t        *trees[129]; /* for 0..128, inclusive. */
47         int             min_prefix;
48 };
49
50
51 #ifdef WITH_STATS
52 static rbtree_t         *tree_num = NULL;     /* client numbers 0..N */
53 static int              tree_num_max = 0;
54 #endif
55 static RADCLIENT_LIST   *root_clients = NULL;
56
57 #ifdef WITH_DYNAMIC_CLIENTS
58 static fr_fifo_t        *deleted_clients = NULL;
59 #endif
60
61 /*
62  *      Callback for freeing a client.
63  */
64 void client_free(RADCLIENT *client)
65 {
66 #ifdef WITH_DYNAMIC_CLIENTS
67         if (client->dynamic == 2) {
68                 time_t now;
69
70                 if (!deleted_clients) {
71                         deleted_clients = fr_fifo_create(1024,
72                                                          (void *) client_free);
73                         if (!deleted_clients) return; /* MEMLEAK */
74                 }
75
76                 /*
77                  *      Mark it as in the fifo, and remember when we
78                  *      pushed it.
79                  */
80                 client->dynamic = 3;
81                 client->created = now = time(NULL); /* re-set it */
82                 fr_fifo_push(deleted_clients, client);
83
84                 /*
85                  *      Peek at the head of the fifo.  If it might
86                  *      still be in use, return.  Otherwise, pop it
87                  *      from the queue and delete it.
88                  */
89                 client = fr_fifo_peek(deleted_clients);
90                 if ((client->created + 120) >= now) return;
91
92                 client = fr_fifo_pop(deleted_clients);
93                 rad_assert(client != NULL);
94         }
95 #endif
96
97         free(client->longname);
98         free(client->secret);
99         free(client->shortname);
100         free(client->nastype);
101         free(client->login);
102         free(client->password);
103         free(client->server);
104
105 #ifdef WITH_STATS
106         free(client->auth);
107 #ifdef WITH_ACCOUNTING
108         free(client->acct);
109 #endif
110 #endif
111
112 #ifdef WITH_DYNAMIC_CLIENTS
113         free(client->client_server);
114 #endif
115
116         free(client);
117 }
118
119 /*
120  *      Callback for comparing two clients.
121  */
122 static int client_ipaddr_cmp(const void *one, const void *two)
123 {
124         const RADCLIENT *a = one;
125         const RADCLIENT *b = two;
126 #ifndef WITH_TCP
127
128         return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
129 #else
130         int rcode;
131
132         rcode = fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
133         if (rcode != 0) return rcode;
134
135         /*
136          *      Wildcard match
137          */
138         if ((a->proto == IPPROTO_IP) ||
139             (b->proto == IPPROTO_IP)) return 0;
140
141         return (a->proto - b->proto);
142 #endif
143 }
144
145 #ifdef WITH_STATS
146 static int client_num_cmp(const void *one, const void *two)
147 {
148         const RADCLIENT *a = one;
149         const RADCLIENT *b = two;
150
151         return (a->number - b->number);
152 }
153 #endif
154
155 /*
156  *      Free a RADCLIENT list.
157  */
158 void clients_free(RADCLIENT_LIST *clients)
159 {
160         int i;
161
162         if (!clients) return;
163
164         for (i = 0; i <= 128; i++) {
165                 if (clients->trees[i]) rbtree_free(clients->trees[i]);
166                 clients->trees[i] = NULL;
167         }
168
169         if (clients == root_clients) {
170 #ifdef WITH_STATS
171                 if (tree_num) rbtree_free(tree_num);
172                 tree_num = NULL;
173                 tree_num_max = 0;
174 #endif
175                 root_clients = NULL;
176         }
177
178 #ifdef WITH_DYNAMIC_CLIENTS
179         /*
180          *      FIXME: No fr_fifo_delete()
181          */
182 #endif
183
184         free(clients);
185 }
186
187 /*
188  *      Return a new, initialized, set of clients.
189  */
190 RADCLIENT_LIST *clients_init(void)
191 {
192         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
193
194         if (!clients) return NULL;
195
196         clients->min_prefix = 128;
197
198         return clients;
199 }
200
201
202 /*
203  *      Sanity check a client.
204  */
205 static int client_sane(RADCLIENT *client)
206 {
207         switch (client->ipaddr.af) {
208         case AF_INET:
209                 if (client->prefix > 32) {
210                         return 0;
211                 }
212
213                 /*
214                  *      Zero out the subnet bits.
215                  */
216                 if (client->prefix == 0) {
217                         memset(&client->ipaddr.ipaddr.ip4addr, 0,
218                                sizeof(client->ipaddr.ipaddr.ip4addr));
219
220                 } else if (client->prefix < 32) {
221                         uint32_t mask = ~0;
222
223                         mask <<= (32 - client->prefix);
224                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
225                 }
226                 break;
227
228         case AF_INET6:
229                 if (client->prefix > 128) return 0;
230
231                 if (client->prefix == 0) {
232                         memset(&client->ipaddr.ipaddr.ip6addr, 0,
233                                sizeof(client->ipaddr.ipaddr.ip6addr));
234
235                 } else if (client->prefix < 128) {
236                         uint32_t mask, *addr;
237
238                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
239                         mask = ~ ((uint32_t) 0);
240                         mask <<= (32 - (client->prefix & 0x1f));
241                         mask = htonl(mask);
242
243                         switch ((client->prefix - 1) >> 5) {
244                         case 0:
245                                 addr[0] &= mask;
246                                 mask = 0;
247                                 /* FALL-THROUGH */
248                         case 1:
249                                 addr[1] &= mask;
250                                 mask = 0;
251                                 /* FALL-THROUGH */
252                         case 2:
253                                 addr[2] &= mask;
254                                 mask = 0;
255                                 /* FALL-THROUGH */
256                         case 3:
257                                 addr[3] &= mask;
258                           break;
259                         }
260                 }
261                 break;
262
263         default:
264                 return 0;
265         }
266
267         return 1;
268 }
269
270
271 /*
272  *      Add a client to the tree.
273  */
274 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
275 {
276         RADCLIENT *old;
277
278         if (!client) {
279                 return 0;
280         }
281
282         /*
283          *      If "clients" is NULL, it means add to the global list.
284          */
285         if (!clients) {
286                 /*
287                  *      Initialize it, if not done already.
288                  */
289                 if (!root_clients) {
290                         root_clients = clients_init();
291                         if (!root_clients) return 0;
292                 }
293                 clients = root_clients;
294         }
295
296         if ((client->prefix < 0) || (client->prefix > 128)) {
297                 return 0;
298         }
299
300         if (!client_sane(client)) return 0;
301
302         /*
303          *      Create a tree for it.
304          */
305         if (!clients->trees[client->prefix]) {
306                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
307                                                                (void *) client_free, 0);
308                 if (!clients->trees[client->prefix]) {
309                         return 0;
310                 }
311         }
312
313 #define namecmp(a) ((!old->a && !client->a) || (old->a && client->a && (strcmp(old->a, client->a) == 0)))
314
315         /*
316          *      Cannot insert the same client twice.
317          */
318         old = rbtree_finddata(clients->trees[client->prefix], client);
319         if (old) {
320                 /*
321                  *      If it's a complete duplicate, then free the new
322                  *      one, and return "OK".
323                  */
324                 if ((fr_ipaddr_cmp(&old->ipaddr, &client->ipaddr) == 0) &&
325                     (old->prefix == client->prefix) &&
326                     namecmp(longname) && namecmp(secret) &&
327                     namecmp(shortname) && namecmp(nastype) &&
328                     namecmp(login) && namecmp(password) && namecmp(server) &&
329 #ifdef WITH_DYNAMIC_CLIENTS
330                     (old->lifetime == client->lifetime) &&
331                     namecmp(client_server) &&
332 #endif
333 #ifdef WITH_COA
334                     namecmp(coa_name) &&
335                     (old->coa_server == client->coa_server) &&
336                     (old->coa_pool == client->coa_pool) &&
337 #endif
338                     (old->message_authenticator == client->message_authenticator)) {
339                         DEBUG("WARNING: Ignoring duplicate client %s", client->longname);
340                         client_free(client);
341                         return 1;
342                 }
343
344                 radlog(L_ERR, "Failed to add duplicate client %s",
345                        client->shortname);
346                 return 0;
347         }
348 #undef namecmp
349
350         /*
351          *      Other error adding client: likely is fatal.
352          */
353         if (!rbtree_insert(clients->trees[client->prefix], client)) {
354                 return 0;
355         }
356
357 #ifdef WITH_STATS
358         if (!tree_num) {
359                 tree_num = rbtree_create(client_num_cmp, NULL, 0);
360         }
361
362
363         /*
364          *      Catch clients added by rlm_sql.
365          */
366         if (!client->auth) {
367                 client->auth = rad_malloc(sizeof(*client->auth));
368                 memset(client->auth, 0, sizeof(*client->auth));
369         }
370
371 #ifdef WITH_ACCOUNTING
372         if (!client->acct) {
373                 client->acct = rad_malloc(sizeof(*client->acct));
374                 memset(client->acct, 0, sizeof(*client->acct));
375         }
376 #endif
377
378 #ifdef WITH_DYNAMIC_CLIENTS
379         /*
380          *      More catching of clients added by rlm_sql.
381          *
382          *      The sql modules sets the dynamic flag BEFORE calling
383          *      us.  The client_create() function sets it AFTER
384          *      calling us.
385          */
386         if (client->dynamic && (client->lifetime == 0)) {
387                 RADCLIENT *network;
388
389                 /*
390                  *      If there IS an enclosing network,
391                  *      inherit the lifetime from it.
392                  */
393                 network = client_find(clients, &client->ipaddr, client->proto);
394                 if (network) {
395                         client->lifetime = network->lifetime;
396                 }
397         }
398 #endif
399
400         client->number = tree_num_max;
401         tree_num_max++;
402         if (tree_num) rbtree_insert(tree_num, client);
403 #endif
404
405         if (client->prefix < clients->min_prefix) {
406                 clients->min_prefix = client->prefix;
407         }
408
409         return 1;
410 }
411
412
413 #ifdef WITH_DYNAMIC_CLIENTS
414 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
415 {
416         if (!clients || !client) return;
417
418         rad_assert((client->prefix >= 0) && (client->prefix <= 128));
419
420         client->dynamic = 2;    /* signal to client_free */
421
422         rbtree_deletebydata(tree_num, client);
423         rbtree_deletebydata(clients->trees[client->prefix], client);
424 }
425 #endif
426
427
428 /*
429  *      Find a client in the RADCLIENTS list by number.
430  *      This is a support function for the statistics code.
431  */
432 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
433                                int number)
434 {
435 #ifdef WITH_STATS
436         if (!clients) clients = root_clients;
437
438         if (!clients) return NULL;
439
440         if (number >= tree_num_max) return NULL;
441
442         if (tree_num) {
443                 RADCLIENT myclient;
444
445                 myclient.number = number;
446
447                 return rbtree_finddata(tree_num, &myclient);
448         }
449 #else
450         clients = clients;      /* -Wunused */
451         number = number;        /* -Wunused */
452 #endif
453         return NULL;
454 }
455
456
457 /*
458  *      Find a client in the RADCLIENTS list.
459  */
460 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
461                        const fr_ipaddr_t *ipaddr, int proto)
462 {
463         int i, max_prefix;
464         RADCLIENT myclient;
465
466         if (!clients) clients = root_clients;
467
468         if (!clients || !ipaddr) return NULL;
469
470         switch (ipaddr->af) {
471         case AF_INET:
472                 max_prefix = 32;
473                 break;
474
475         case AF_INET6:
476                 max_prefix = 128;
477                 break;
478
479         default :
480                 return NULL;
481         }
482
483         for (i = max_prefix; i >= clients->min_prefix; i--) {
484                 void *data;
485
486                 myclient.prefix = i;
487                 myclient.ipaddr = *ipaddr;
488                 myclient.proto = proto;
489                 client_sane(&myclient); /* clean up the ipaddress */
490
491                 if (!clients->trees[i]) continue;
492
493                 data = rbtree_finddata(clients->trees[i], &myclient);
494                 if (data) {
495                         return data;
496                 }
497         }
498
499         return NULL;
500 }
501
502
503 /*
504  *      Old wrapper for client_find
505  */
506 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
507 {
508         return client_find(root_clients, ipaddr, IPPROTO_UDP);
509 }
510
511 static struct in_addr cl_ip4addr;
512 static struct in6_addr cl_ip6addr;
513 #ifdef WITH_TCP
514 static char *hs_proto = NULL;
515 #endif
516
517 static const CONF_PARSER client_config[] = {
518         { "ipaddr",  PW_TYPE_IPADDR,
519           0, &cl_ip4addr,  NULL },
520         { "ipv6addr",  PW_TYPE_IPV6ADDR,
521           0, &cl_ip6addr, NULL },
522         { "netmask",  PW_TYPE_INTEGER,
523           offsetof(RADCLIENT, prefix), 0, NULL },
524
525         { "require_message_authenticator",  PW_TYPE_BOOLEAN,
526           offsetof(RADCLIENT, message_authenticator), 0, "no" },
527
528         { "secret",  PW_TYPE_STRING_PTR,
529           offsetof(RADCLIENT, secret), 0, NULL },
530         { "shortname",  PW_TYPE_STRING_PTR,
531           offsetof(RADCLIENT, shortname), 0, NULL },
532         { "nastype",  PW_TYPE_STRING_PTR,
533           offsetof(RADCLIENT, nastype), 0, NULL },
534         { "login",  PW_TYPE_STRING_PTR,
535           offsetof(RADCLIENT, login), 0, NULL },
536         { "password",  PW_TYPE_STRING_PTR,
537           offsetof(RADCLIENT, password), 0, NULL },
538         { "virtual_server",  PW_TYPE_STRING_PTR,
539           offsetof(RADCLIENT, server), 0, NULL },
540         { "server",  PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
541           offsetof(RADCLIENT, server), 0, NULL },
542
543 #ifdef WITH_TCP
544         { "proto",  PW_TYPE_STRING_PTR,
545           0, &hs_proto, NULL },
546         { "max_connections",  PW_TYPE_INTEGER,
547           offsetof(RADCLIENT, max_connections), 0, "16" },
548 #endif
549
550 #ifdef WITH_DYNAMIC_CLIENTS
551         { "dynamic_clients",  PW_TYPE_STRING_PTR,
552           offsetof(RADCLIENT, client_server), 0, NULL },
553         { "lifetime",  PW_TYPE_INTEGER,
554           offsetof(RADCLIENT, lifetime), 0, NULL },
555 #endif
556
557 #ifdef WITH_COA
558         { "coa_server",  PW_TYPE_STRING_PTR,
559           offsetof(RADCLIENT, coa_name), 0, NULL },
560 #endif
561
562         { NULL, -1, 0, NULL, NULL }
563 };
564
565
566 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
567 {
568         RADCLIENT       *c;
569         const char      *name2;
570
571         name2 = cf_section_name2(cs);
572         if (!name2) {
573                 cf_log_err(cf_sectiontoitem(cs),
574                            "Missing client name");
575                 return NULL;
576         }
577
578         /*
579          * The size is fine.. Let's create the buffer
580          */
581         c = rad_malloc(sizeof(*c));
582         memset(c, 0, sizeof(*c));
583         c->cs = cs;
584
585 #ifdef WITH_STATS
586         c->auth = rad_malloc(sizeof(*c->auth));
587         memset(c->auth, 0, sizeof(*c->auth));
588
589 #ifdef WITH_ACCOUNTING
590         c->acct = rad_malloc(sizeof(*c->acct));
591         memset(c->acct, 0, sizeof(*c->acct));
592 #endif
593 #endif
594
595         memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
596         memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
597         c->prefix = -1;
598
599         if (cf_section_parse(cs, c, client_config) < 0) {
600                 cf_log_err(cf_sectiontoitem(cs),
601                            "Error parsing client section.");
602         error:
603                 client_free(c);
604 #ifdef WITH_TCP
605                 free(hs_proto);
606                 hs_proto = NULL;
607 #endif
608
609                 return NULL;
610         }
611
612         /*
613          *      Global clients can set servers to use,
614          *      per-server clients cannot.
615          */
616         if (in_server && c->server) {
617                 cf_log_err(cf_sectiontoitem(cs),
618                            "Clients inside of an server section cannot point to a server.");
619                 goto error;
620         }
621                 
622         /*
623          *      No "ipaddr" or "ipv6addr", use old-style
624          *      "client <ipaddr> {" syntax.
625          */
626         if (!cf_pair_find(cs, "ipaddr") &&
627             !cf_pair_find(cs, "ipv6addr")) {
628                 char *prefix_ptr;
629
630                 prefix_ptr = strchr(name2, '/');
631
632                 /*
633                  *      Look for prefixes.
634                  */
635                 if (prefix_ptr) {
636                         c->prefix = atoi(prefix_ptr + 1);
637                         if ((c->prefix < 0) || (c->prefix > 128)) {
638                                 cf_log_err(cf_sectiontoitem(cs),
639                                            "Invalid Prefix value '%s' for IP.",
640                                            prefix_ptr + 1);
641                                 goto error;
642                         }
643                         /* Replace '/' with '\0' */
644                         *prefix_ptr = '\0';
645                 }
646                         
647                 /*
648                  *      Always get the numeric representation of IP
649                  */
650                 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
651                         cf_log_err(cf_sectiontoitem(cs),
652                                    "Failed to look up hostname %s: %s",
653                                    name2, fr_strerror());
654                         goto error;
655                 }
656
657                 if (prefix_ptr) *prefix_ptr = '/';
658                 c->longname = strdup(name2);
659
660                 if (!c->shortname) c->shortname = strdup(c->longname);
661
662         } else {
663                 char buffer[1024];
664
665                 /*
666                  *      Figure out which one to use.
667                  */
668                 if (cf_pair_find(cs, "ipaddr")) {
669                         c->ipaddr.af = AF_INET;
670                         c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
671
672                         if ((c->prefix < -1) || (c->prefix > 32)) {
673                                 cf_log_err(cf_sectiontoitem(cs),
674                                            "Netmask must be between 0 and 32");
675                                 goto error;
676                         }
677                                 
678                 } else if (cf_pair_find(cs, "ipv6addr")) {
679                         c->ipaddr.af = AF_INET6;
680                         c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
681                                 
682                         if ((c->prefix < -1) || (c->prefix > 128)) {
683                                 cf_log_err(cf_sectiontoitem(cs),
684                                            "Netmask must be between 0 and 128");
685                                 goto error;
686                         }
687                 } else {
688                         cf_log_err(cf_sectiontoitem(cs),
689                                    "No IP address defined for the client");
690                         goto error;
691                 }
692
693                 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
694                 c->longname = strdup(buffer);
695
696                 /*
697                  *      Set the short name to the name2
698                  */
699                 if (!c->shortname) c->shortname = strdup(name2);
700
701                 c->proto = IPPROTO_UDP;
702 #ifdef WITH_TCP
703                 if (hs_proto) {
704                         if (strcmp(hs_proto, "udp") == 0) {
705                                 free(hs_proto);
706                                 hs_proto = NULL;
707                                 
708                         } else if (strcmp(hs_proto, "tcp") == 0) {
709                                 free(hs_proto);
710                                 hs_proto = NULL;
711                                 c->proto = IPPROTO_TCP;
712                                 
713                         } else if (strcmp(hs_proto, "*") == 0) {
714                                 free(hs_proto);
715                                 hs_proto = NULL;
716                                 c->proto = IPPROTO_IP; /* fake for dual */
717                                 
718                         } else {
719                                 cf_log_err(cf_sectiontoitem(cs),
720                                            "Unknown proto \"%s\".", hs_proto);
721                                 goto error;
722                         }
723                 }
724 #endif
725         }
726
727         if (c->prefix < 0) switch (c->ipaddr.af) {
728         case AF_INET:
729                 c->prefix = 32;
730                 break;
731         case AF_INET6:
732                 c->prefix = 128;
733                 break;
734         default:
735                 break;
736         }
737
738 #ifdef WITH_DYNAMIC_CLIENTS
739         if (c->client_server) {
740                 free(c->secret);
741                 c->secret = strdup("testing123");
742
743                 if (((c->ipaddr.af == AF_INET) &&
744                      (c->prefix == 32)) ||
745                     ((c->ipaddr.af == AF_INET6) &&
746                      (c->prefix == 128))) {
747                         cf_log_err(cf_sectiontoitem(cs),
748                                    "Dynamic clients MUST be a network, not a single IP address.");
749                         goto error;
750                 }
751
752                 return c;
753         }
754 #endif
755
756         if (!c->secret || !*c->secret) {
757 #ifdef WITH_DHCP
758                 const char *value = NULL;
759                 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
760
761                 if (cp) value = cf_pair_value(cp);
762
763                 /*
764                  *      Secrets aren't needed for DHCP.
765                  */
766                 if (value && (strcmp(value, "yes") == 0)) return c;
767
768 #endif
769                 cf_log_err(cf_sectiontoitem(cs),
770                            "secret must be at least 1 character long");
771                 goto error;
772         }
773
774 #ifdef WITH_COA
775         /*
776          *      Point the client to the home server pool, OR to the
777          *      home server.  This gets around the problem of figuring
778          *      out which port to use.
779          */
780         if (c->coa_name) {
781                 c->coa_pool = home_pool_byname(c->coa_name, HOME_TYPE_COA);
782                 if (!c->coa_pool) {
783                         c->coa_server = home_server_byname(c->coa_name,
784                                                            HOME_TYPE_COA);
785                 }
786                 if (!c->coa_pool && !c->coa_server) {
787                         cf_log_err(cf_sectiontoitem(cs), "No such home_server or home_server_pool \"%s\"", c->coa_name);
788                         goto error;
789                 }
790         }
791 #endif
792
793         return c;
794 }
795
796
797 /*
798  *      Create the linked list of clients from the new configuration
799  *      type.  This way we don't have to change too much in the other
800  *      source-files.
801  */
802 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
803 {
804         int             global = FALSE, in_server = FALSE;
805         CONF_SECTION    *cs;
806         RADCLIENT       *c;
807         RADCLIENT_LIST  *clients;
808
809         /*
810          *      Be forgiving.  If there's already a clients, return
811          *      it.  Otherwise create a new one.
812          */
813         clients = cf_data_find(section, "clients");
814         if (clients) return clients;
815
816         clients = clients_init();
817         if (!clients) return NULL;
818
819         if (cf_top_section(section) == section) global = TRUE;
820
821         if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
822
823         /*
824          *      Associate the clients structure with the section, where
825          *      it will be freed once the section is freed.
826          */
827         if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
828                 cf_log_err(cf_sectiontoitem(section),
829                            "Failed to associate clients with section %s",
830                        cf_section_name1(section));
831                 clients_free(clients);
832                 return NULL;
833         }
834
835         for (cs = cf_subsection_find_next(section, NULL, "client");
836              cs != NULL;
837              cs = cf_subsection_find_next(section, cs, "client")) {
838                 c = client_parse(cs, in_server);
839                 if (!c) {
840                         return NULL;
841                 }
842
843                 /*
844                  *      FIXME: Add the client as data via cf_data_add,
845                  *      for migration issues.
846                  */
847
848 #ifdef WITH_DYNAMIC_CLIENTS
849 #ifdef HAVE_DIRENT_H
850                 if (c->client_server) {
851                         const char *value;
852                         CONF_PAIR *cp;
853                         DIR             *dir;
854                         struct dirent   *dp;
855                         struct stat stat_buf;
856                         char buf2[2048];
857
858                         /*
859                          *      Find the directory where individual
860                          *      client definitions are stored.
861                          */
862                         cp = cf_pair_find(cs, "directory");
863                         if (!cp) goto add_client;
864                         
865                         value = cf_pair_value(cp);
866                         if (!value) {
867                                 cf_log_err(cf_sectiontoitem(cs),
868                                            "The \"directory\" entry must not be empty");
869                                 client_free(c);
870                                 return NULL;
871                         }
872
873                         DEBUG("including dynamic clients in %s", value);
874                         
875                         dir = opendir(value);
876                         if (!dir) {
877                                 cf_log_err(cf_sectiontoitem(cs), "Error reading directory %s: %s", value, strerror(errno));
878                                 client_free(c);
879                                 return NULL;
880                         }
881                         
882                         /*
883                          *      Read the directory, ignoring "." files.
884                          */
885                         while ((dp = readdir(dir)) != NULL) {
886                                 const char *p;
887                                 RADCLIENT *dc;
888
889                                 if (dp->d_name[0] == '.') continue;
890
891                                 /*
892                                  *      Check for valid characters
893                                  */
894                                 for (p = dp->d_name; *p != '\0'; p++) {
895                                         if (isalpha((int)*p) ||
896                                             isdigit((int)*p) ||
897                                             (*p == ':') ||
898                                             (*p == '.')) continue;
899                                                 break;
900                                 }
901                                 if (*p != '\0') continue;
902
903                                 snprintf(buf2, sizeof(buf2), "%s/%s",
904                                          value, dp->d_name);
905
906                                 if ((stat(buf2, &stat_buf) != 0) ||
907                                     S_ISDIR(stat_buf.st_mode)) continue;
908
909                                 dc = client_read(buf2, in_server, TRUE);
910                                 if (!dc) {
911                                         cf_log_err(cf_sectiontoitem(cs),
912                                                    "Failed reading client file \"%s\"", buf2);
913                                         client_free(c);
914                                         return NULL;
915                                 }
916
917                                 /*
918                                  *      Validate, and add to the list.
919                                  */
920                                 if (!client_validate(clients, c, dc)) {
921                                         
922                                         client_free(c);
923                                         return NULL;
924                                 }
925                         } /* loop over the directory */
926                 }
927 #endif /* HAVE_DIRENT_H */
928 #endif /* WITH_DYNAMIC_CLIENTS */
929
930         add_client:
931                 if (!client_add(clients, c)) {
932                         cf_log_err(cf_sectiontoitem(cs),
933                                    "Failed to add client %s",
934                                    cf_section_name2(cs));
935                         client_free(c);
936                         return NULL;
937                 }
938
939         }
940
941         /*
942          *      Replace the global list of clients with the new one.
943          *      The old one is still referenced from the original
944          *      configuration, and will be freed when that is freed.
945          */
946         if (global) {
947                 root_clients = clients;
948         }
949
950         return clients;
951 }
952
953 #ifdef WITH_DYNAMIC_CLIENTS
954 /*
955  *      We overload this structure a lot.
956  */
957 static const CONF_PARSER dynamic_config[] = {
958         { "FreeRADIUS-Client-IP-Address",  PW_TYPE_IPADDR,
959           offsetof(RADCLIENT, ipaddr), 0, NULL },
960         { "FreeRADIUS-Client-IPv6-Address",  PW_TYPE_IPV6ADDR,
961           offsetof(RADCLIENT, ipaddr), 0, NULL },
962
963         { "FreeRADIUS-Client-Require-MA",  PW_TYPE_BOOLEAN,
964           offsetof(RADCLIENT, message_authenticator), NULL, NULL },
965
966         { "FreeRADIUS-Client-Secret",  PW_TYPE_STRING_PTR,
967           offsetof(RADCLIENT, secret), 0, "" },
968         { "FreeRADIUS-Client-Shortname",  PW_TYPE_STRING_PTR,
969           offsetof(RADCLIENT, shortname), 0, "" },
970         { "FreeRADIUS-Client-NAS-Type",  PW_TYPE_STRING_PTR,
971           offsetof(RADCLIENT, nastype), 0, NULL },
972         { "FreeRADIUS-Client-Virtual-Server",  PW_TYPE_STRING_PTR,
973           offsetof(RADCLIENT, server), 0, NULL },
974
975         { NULL, -1, 0, NULL, NULL }
976 };
977
978
979 int client_validate(RADCLIENT_LIST *clients, RADCLIENT *master, RADCLIENT *c)
980 {
981         char buffer[128];
982
983         /*
984          *      No virtual server defined.  Inherit the parent's
985          *      definition.
986          */
987         if (master->server && !c->server) {
988                 c->server = strdup(master->server);
989         }
990
991         /*
992          *      If the client network isn't global (not tied to a
993          *      virtual server), then ensure that this clients server
994          *      is the same as the enclosing networks virtual server.
995          */
996         if (master->server &&
997              (strcmp(master->server, c->server) != 0)) {
998                 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
999                       ip_ntoh(&c->ipaddr,
1000                               buffer, sizeof(buffer)),
1001                       c->server);
1002
1003                 goto error;
1004         }
1005
1006         if (!client_add(clients, c)) {
1007                 DEBUG("- Cannot add client %s: Internal error",
1008                       ip_ntoh(&c->ipaddr,
1009                               buffer, sizeof(buffer)));
1010
1011                 goto error;
1012         }
1013
1014         /*
1015          *      Initialize the remaining fields.
1016          */
1017         c->dynamic = TRUE;
1018         c->lifetime = master->lifetime;
1019         c->created = time(NULL);
1020         c->longname = strdup(c->shortname);
1021
1022         DEBUG("- Added client %s with shared secret %s",
1023               ip_ntoh(&c->ipaddr, buffer, sizeof(buffer)),
1024               c->secret);
1025
1026         return 1;
1027
1028  error:
1029         client_free(c);
1030         return 0;
1031 }
1032
1033
1034 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
1035 {
1036         int i, *pi;
1037         char **p;
1038         RADCLIENT *c;
1039         char buffer[128];
1040
1041         if (!clients || !request) return NULL;
1042
1043         c = rad_malloc(sizeof(*c));
1044         memset(c, 0, sizeof(*c));
1045         c->cs = request->client->cs;
1046         c->ipaddr.af = AF_UNSPEC;
1047
1048         for (i = 0; dynamic_config[i].name != NULL; i++) {
1049                 DICT_ATTR *da;
1050                 VALUE_PAIR *vp;
1051
1052                 da = dict_attrbyname(dynamic_config[i].name);
1053                 if (!da) {
1054                         DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
1055                               ip_ntoh(&request->packet->src_ipaddr,
1056                                       buffer, sizeof(buffer)),
1057                               dynamic_config[i].name);
1058                 error:
1059                         client_free(c);
1060                         return NULL;
1061                 }
1062
1063                 vp = pairfind(request->config_items, da->attr);
1064                 if (!vp) {
1065                         /*
1066                          *      Not required.  Skip it.
1067                          */
1068                         if (!dynamic_config[i].dflt) continue;
1069                         
1070                         DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",  
1071                               ip_ntoh(&request->packet->src_ipaddr,
1072                                       buffer, sizeof(buffer)),
1073                               dynamic_config[i].name);
1074                         goto error;
1075                 }
1076
1077                 switch (dynamic_config[i].type) {
1078                 case PW_TYPE_IPADDR:
1079                         c->ipaddr.af = AF_INET;
1080                         c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
1081                         c->prefix = 32;
1082                         break;
1083
1084                 case PW_TYPE_IPV6ADDR:
1085                         c->ipaddr.af = AF_INET6;
1086                         c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
1087                         c->prefix = 128;
1088                         break;
1089
1090                 case PW_TYPE_STRING_PTR:
1091                         p = (char **) ((char *) c + dynamic_config[i].offset);
1092                         if (*p) free(*p);
1093                         *p = strdup(vp->vp_strvalue);
1094                         break;
1095
1096                 case PW_TYPE_BOOLEAN:
1097                         pi = (int *) ((char *) c + dynamic_config[i].offset);
1098                         *pi = vp->vp_integer;
1099                         break;
1100
1101                 default:
1102                         goto error;
1103                 }
1104         }
1105
1106         if (c->ipaddr.af == AF_UNSPEC) {
1107                 DEBUG("- Cannot add client %s: No IP address was specified.",
1108                       ip_ntoh(&request->packet->src_ipaddr,
1109                               buffer, sizeof(buffer)));
1110
1111                 goto error;
1112         }
1113
1114         if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
1115                 char buf2[128];
1116
1117                 DEBUG("- Cannot add client %s: IP address %s do not match",
1118                       ip_ntoh(&request->packet->src_ipaddr,
1119                               buffer, sizeof(buffer)),
1120                       ip_ntoh(&c->ipaddr,
1121                               buf2, sizeof(buf2)));                   
1122                 goto error;
1123         }
1124
1125         if (!client_validate(clients, request->client, c)) {
1126                 return NULL;
1127         }
1128
1129         return c;
1130 }
1131
1132 /*
1133  *      Read a client definition from the given filename.
1134  */
1135 RADCLIENT *client_read(const char *filename, int in_server, int flag)
1136 {
1137         const char *p;
1138         RADCLIENT *c;
1139         CONF_SECTION *cs;
1140         char buffer[256];
1141
1142         if (!filename) return NULL;
1143
1144         cs = cf_file_read(filename);
1145         if (!cs) return NULL;
1146
1147         c = client_parse(cf_section_sub_find(cs, "client"), in_server);
1148
1149         p = strrchr(filename, FR_DIR_SEP);
1150         if (p) {
1151                 p++;
1152         } else {
1153                 p = filename;
1154         }
1155
1156         if (!flag) return c;
1157
1158         /*
1159          *      Additional validations
1160          */
1161         ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
1162         if (strcmp(p, buffer) != 0) {
1163                 DEBUG("Invalid client definition in %s: IP address %s does not match name %s", filename, buffer, p);
1164                 client_free(c);
1165                 return NULL;
1166         }
1167
1168
1169
1170         return c;
1171 }
1172 #endif