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