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