1f2a5be822cf3a8dc8d1fd5adde48c49a7116d3c
[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 struct radclient_list {
37         /*
38          *      FIXME: One set of trees for IPv4, and another for IPv6?
39          */
40         rbtree_t        *trees[129]; /* for 0..128, inclusive. */
41         int             min_prefix;
42 };
43
44
45 #ifdef WITH_STATS
46 static rbtree_t         *tree_num;      /* client numbers 0..N */
47 static int              tree_num_max;
48 #endif
49 static RADCLIENT_LIST   *root_clients;
50
51 /*
52  *      Callback for freeing a client.
53  */
54 void client_free(RADCLIENT *client)
55 {
56         free(client->longname);
57         free(client->secret);
58         free(client->shortname);
59         free(client->nastype);
60         free(client->login);
61         free(client->password);
62         free(client->server);
63
64 #ifdef WITH_STATS
65         free(client->auth);
66 #ifdef WITH_ACCOUNTING
67         free(client->acct);
68 #endif
69 #endif
70
71 #ifdef WITH_DYNAMIC_CLIENTS
72         free(client->client_server);
73 #endif
74
75         free(client);
76 }
77
78 /*
79  *      Callback for comparing two clients.
80  */
81 static int client_ipaddr_cmp(const void *one, const void *two)
82 {
83         const RADCLIENT *a = one;
84         const RADCLIENT *b = two;
85
86         return fr_ipaddr_cmp(&a->ipaddr, &b->ipaddr);
87 }
88
89 #ifdef WITH_STATS
90 static int client_num_cmp(const void *one, const void *two)
91 {
92         const RADCLIENT *a = one;
93         const RADCLIENT *b = two;
94
95         return (a->number - b->number);
96 }
97 #endif
98
99 /*
100  *      Free a RADCLIENT list.
101  */
102 void clients_free(RADCLIENT_LIST *clients)
103 {
104         int i;
105
106         if (!clients) return;
107
108         for (i = 0; i <= 128; i++) {
109                 if (clients->trees[i]) rbtree_free(clients->trees[i]);
110                 clients->trees[i] = NULL;
111         }
112
113         if (clients == root_clients) {
114 #ifdef WITH_STATS
115                 if (tree_num) rbtree_free(tree_num);
116                 tree_num = NULL;
117                 tree_num_max = 0;
118 #endif
119                 root_clients = NULL;
120         }
121
122         free(clients);
123 }
124
125 /*
126  *      Return a new, initialized, set of clients.
127  */
128 RADCLIENT_LIST *clients_init(void)
129 {
130         RADCLIENT_LIST *clients = calloc(1, sizeof(RADCLIENT_LIST));
131
132         if (!clients) return NULL;
133
134         clients->min_prefix = 128;
135
136         return clients;
137 }
138
139
140 /*
141  *      Sanity check a client.
142  */
143 static int client_sane(RADCLIENT *client)
144 {
145         switch (client->ipaddr.af) {
146         case AF_INET:
147                 if (client->prefix > 32) {
148                         return 0;
149                 }
150
151                 /*
152                  *      Zero out the subnet bits.
153                  */
154                 if (client->prefix == 0) {
155                         memset(&client->ipaddr.ipaddr.ip4addr, 0,
156                                sizeof(client->ipaddr.ipaddr.ip4addr));
157
158                 } else if (client->prefix < 32) {
159                         uint32_t mask = ~0;
160
161                         mask <<= (32 - client->prefix);
162                         client->ipaddr.ipaddr.ip4addr.s_addr &= htonl(mask);
163                 }
164                 break;
165
166         case AF_INET6:
167                 if (client->prefix > 128) return 0;
168
169                 if (client->prefix == 0) {
170                         memset(&client->ipaddr.ipaddr.ip6addr, 0,
171                                sizeof(client->ipaddr.ipaddr.ip6addr));
172
173                 } else if (client->prefix < 128) {
174                         int i;
175                         uint32_t mask, *addr;
176
177                         addr = (uint32_t *) &client->ipaddr.ipaddr.ip6addr;
178
179                         for (i = client->prefix; i < 128; i += 32) {
180                                 mask = ~0;
181                                 mask <<= ((128 - i) & 0x1f);
182                                 addr[i / 32] &= mask;
183                         }
184                 }
185                 break;
186
187         default:
188                 return 0;
189         }
190
191         return 1;
192 }
193
194
195 /*
196  *      Add a client to the tree.
197  */
198 int client_add(RADCLIENT_LIST *clients, RADCLIENT *client)
199 {
200         if (!client) {
201                 return 0;
202         }
203
204         /*
205          *      If "clients" is NULL, it means add to the global list.
206          */
207         if (!clients) {
208                 /*
209                  *      Initialize it, if not done already.
210                  */
211                 if (!root_clients) {
212                         root_clients = clients_init();
213                         if (!root_clients) return 0;
214                 }
215                 clients = root_clients;
216         }
217
218         if ((client->prefix < 0) || (client->prefix > 128)) {
219                 return 0;
220         }
221
222         if (!client_sane(client)) return 0;
223
224         /*
225          *      Create a tree for it.
226          */
227         if (!clients->trees[client->prefix]) {
228                 clients->trees[client->prefix] = rbtree_create(client_ipaddr_cmp,
229                                                                (void *) client_free, 0);
230                 if (!clients->trees[client->prefix]) {
231                         return 0;
232                 }
233         }
234
235         /*
236          *      Cannot insert the same client twice.
237          */
238         if (rbtree_find(clients->trees[client->prefix], client)) {
239                 radlog(L_ERR, "Failed to add duplicate client %s",
240                        client->shortname);
241                 return 0;
242         }
243
244         /*
245          *      Other error adding client: likely is fatal.
246          */
247         if (!rbtree_insert(clients->trees[client->prefix], client)) {
248                 return 0;
249         }
250
251 #ifdef WITH_STATS
252         if (!tree_num) {
253                 tree_num = rbtree_create(client_num_cmp, NULL, 0);
254         }
255
256
257         /*
258          *      Catch clients added by rlm_sql.
259          */
260         if (!client->auth) {
261                 client->auth = rad_malloc(sizeof(*client->auth));
262                 memset(client->auth, 0, sizeof(*client->auth));
263         }
264
265 #ifdef WITH_ACCOUNTING
266         if (!client->acct) {
267                 client->acct = rad_malloc(sizeof(*client->acct));
268                 memset(client->acct, 0, sizeof(*client->acct));
269         }
270 #endif
271
272 #ifdef WITH_DYNAMIC_CLIENTS
273         /*
274          *      More catching of clients added by rlm_sql.
275          *
276          *      The sql modules sets the dynamic flag BEFORE calling
277          *      us.  The client_create() function sets it AFTER
278          *      calling us.
279          */
280         if (client->dynamic && (client->lifetime == 0)) {
281                 RADCLIENT *network;
282
283                 /*
284                  *      If there IS an enclosing network,
285                  *      inherit the lifetime from it.
286                  */
287                 network = client_find(clients, &client->ipaddr);
288                 if (network) {
289                         client->lifetime = network->lifetime;
290                 }
291         }
292 #endif
293
294         client->number = tree_num_max;
295         tree_num_max++;
296         if (tree_num) rbtree_insert(tree_num, client);
297 #endif
298
299         if (client->prefix < clients->min_prefix) {
300                 clients->min_prefix = client->prefix;
301         }
302
303         return 1;
304 }
305
306
307 #ifdef WITH_DYNAMIC_CLIENTS
308 void client_delete(RADCLIENT_LIST *clients, RADCLIENT *client)
309 {
310         if (!clients || !client) return;
311
312         rad_assert((client->prefix >= 0) && (client->prefix <= 128));
313
314         rbtree_deletebydata(clients->trees[client->prefix], client);
315 }
316 #endif
317
318
319 /*
320  *      Find a client in the RADCLIENTS list by number.
321  *      This is a support function for the statistics code.
322  */
323 RADCLIENT *client_findbynumber(const RADCLIENT_LIST *clients,
324                                int number)
325 {
326 #ifdef WITH_STATS
327         if (!clients) clients = root_clients;
328
329         if (!clients) return NULL;
330
331         if (number >= tree_num_max) return NULL;
332
333         if (tree_num) {
334                 RADCLIENT myclient;
335
336                 myclient.number = number;
337
338                 return rbtree_finddata(tree_num, &myclient);
339         }
340 #else
341         clients = clients;      /* -Wunused */
342         number = number;        /* -Wunused */
343 #endif
344         return NULL;
345 }
346
347
348 /*
349  *      Find a client in the RADCLIENTS list.
350  */
351 RADCLIENT *client_find(const RADCLIENT_LIST *clients,
352                        const fr_ipaddr_t *ipaddr)
353 {
354         int i, max_prefix;
355         RADCLIENT myclient;
356
357         if (!clients) clients = root_clients;
358
359         if (!clients || !ipaddr) return NULL;
360
361         switch (ipaddr->af) {
362         case AF_INET:
363                 max_prefix = 32;
364                 break;
365
366         case AF_INET6:
367                 max_prefix = 128;
368                 break;
369
370         default :
371                 return NULL;
372         }
373
374         for (i = max_prefix; i >= clients->min_prefix; i--) {
375                 void *data;
376
377                 myclient.prefix = i;
378                 myclient.ipaddr = *ipaddr;
379                 client_sane(&myclient); /* clean up the ipaddress */
380
381                 if (!clients->trees[i]) continue;
382
383                 data = rbtree_finddata(clients->trees[i], &myclient);
384                 if (data) {
385                         return data;
386                 }
387         }
388
389         return NULL;
390 }
391
392
393 /*
394  *      Old wrapper for client_find
395  */
396 RADCLIENT *client_find_old(const fr_ipaddr_t *ipaddr)
397 {
398         return client_find(root_clients, ipaddr);
399 }
400
401 static struct in_addr cl_ip4addr;
402 static struct in6_addr cl_ip6addr;
403
404 static const CONF_PARSER client_config[] = {
405         { "ipaddr",  PW_TYPE_IPADDR,
406           0, &cl_ip4addr,  NULL },
407         { "ipv6addr",  PW_TYPE_IPV6ADDR,
408           0, &cl_ip6addr, NULL },
409         { "netmask",  PW_TYPE_INTEGER,
410           offsetof(RADCLIENT, prefix), 0, NULL },
411
412         { "require_message_authenticator",  PW_TYPE_BOOLEAN,
413           offsetof(RADCLIENT, message_authenticator), 0, "no" },
414
415         { "secret",  PW_TYPE_STRING_PTR,
416           offsetof(RADCLIENT, secret), 0, NULL },
417         { "shortname",  PW_TYPE_STRING_PTR,
418           offsetof(RADCLIENT, shortname), 0, NULL },
419         { "nastype",  PW_TYPE_STRING_PTR,
420           offsetof(RADCLIENT, nastype), 0, NULL },
421         { "login",  PW_TYPE_STRING_PTR,
422           offsetof(RADCLIENT, login), 0, NULL },
423         { "password",  PW_TYPE_STRING_PTR,
424           offsetof(RADCLIENT, password), 0, NULL },
425         { "virtual_server",  PW_TYPE_STRING_PTR,
426           offsetof(RADCLIENT, server), 0, NULL },
427         { "server",  PW_TYPE_STRING_PTR, /* compatability with 2.0-pre */
428           offsetof(RADCLIENT, server), 0, NULL },
429
430 #ifdef WITH_DYNAMIC_CLIENTS
431         { "dynamic_clients",  PW_TYPE_STRING_PTR,
432           offsetof(RADCLIENT, client_server), 0, NULL },
433         { "lifetime",  PW_TYPE_INTEGER,
434           offsetof(RADCLIENT, lifetime), 0, NULL },
435 #endif
436
437         { NULL, -1, 0, NULL, NULL }
438 };
439
440
441 static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
442 {
443         RADCLIENT       *c;
444         const char      *name2;
445
446         name2 = cf_section_name2(cs);
447         if (!name2) {
448                 cf_log_err(cf_sectiontoitem(cs),
449                            "Missing client name");
450                 return NULL;
451         }
452
453         /*
454          * The size is fine.. Let's create the buffer
455          */
456         c = rad_malloc(sizeof(*c));
457         memset(c, 0, sizeof(*c));
458         c->cs = cs;
459
460 #ifdef WITH_STATS
461         c->auth = rad_malloc(sizeof(*c->auth));
462         memset(c->auth, 0, sizeof(*c->auth));
463
464 #ifdef WITH_ACCOUNTING
465         c->acct = rad_malloc(sizeof(*c->acct));
466         memset(c->acct, 0, sizeof(*c->acct));
467 #endif
468 #endif
469
470         memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
471         memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
472         c->prefix = -1;
473
474         if (cf_section_parse(cs, c, client_config) < 0) {
475                 client_free(c);
476                 cf_log_err(cf_sectiontoitem(cs),
477                            "Error parsing client section.");
478                 return NULL;
479         }
480
481         /*
482          *      Global clients can set servers to use,
483          *      per-server clients cannot.
484          */
485         if (in_server && c->server) {
486                 client_free(c);
487                 cf_log_err(cf_sectiontoitem(cs),
488                            "Clients inside of an server section cannot point to a server.");
489                 return NULL;
490         }
491                 
492         /*
493          *      No "ipaddr" or "ipv6addr", use old-style
494          *      "client <ipaddr> {" syntax.
495          */
496         if (!cf_pair_find(cs, "ipaddr") &&
497             !cf_pair_find(cs, "ipv6addr")) {
498                 char *prefix_ptr;
499
500                 prefix_ptr = strchr(name2, '/');
501
502                 /*
503                  *      Look for prefixes.
504                  */
505                 if (prefix_ptr) {
506                         c->prefix = atoi(prefix_ptr + 1);
507                         if ((c->prefix < 0) || (c->prefix > 128)) {
508                                 client_free(c);
509                                 cf_log_err(cf_sectiontoitem(cs),
510                                            "Invalid Prefix value '%s' for IP.",
511                                            prefix_ptr + 1);
512                                 return NULL;
513                         }
514                         /* Replace '/' with '\0' */
515                         *prefix_ptr = '\0';
516                 }
517                         
518                 /*
519                  *      Always get the numeric representation of IP
520                  */
521                 if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
522                         client_free(c);
523                         cf_log_err(cf_sectiontoitem(cs),
524                                    "Failed to look up hostname %s: %s",
525                                    name2, fr_strerror());
526                         return NULL;
527                 }
528
529                 if (prefix_ptr) *prefix_ptr = '/';
530                 c->longname = strdup(name2);
531
532                 if (!c->shortname) c->shortname = strdup(c->longname);
533
534         } else {
535                 char buffer[1024];
536
537                 /*
538                  *      Figure out which one to use.
539                  */
540                 if (cf_pair_find(cs, "ipaddr")) {
541                         c->ipaddr.af = AF_INET;
542                         c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
543
544                         if ((c->prefix < -1) || (c->prefix > 32)) {
545                                 client_free(c);
546                                 cf_log_err(cf_sectiontoitem(cs),
547                                            "Netmask must be between 0 and 32");
548                                 return NULL;
549                         }
550                                 
551                 } else if (cf_pair_find(cs, "ipv6addr")) {
552                         c->ipaddr.af = AF_INET6;
553                         c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
554                                 
555                         if ((c->prefix < -1) || (c->prefix > 128)) {
556                                 client_free(c);
557                                 cf_log_err(cf_sectiontoitem(cs),
558                                            "Netmask must be between 0 and 128");
559                                 return NULL;
560                         }
561                 } else {
562                         cf_log_err(cf_sectiontoitem(cs),
563                                    "No IP address defined for the client");
564                         client_free(c);
565                         return NULL;
566                 }
567
568                 ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
569                 c->longname = strdup(buffer);
570
571                 /*
572                  *      Set the short name to the name2
573                  */
574                 if (!c->shortname) c->shortname = strdup(name2);
575         }
576
577         if (c->prefix < 0) switch (c->ipaddr.af) {
578         case AF_INET:
579                 c->prefix = 32;
580                 break;
581         case AF_INET6:
582                 c->prefix = 128;
583                 break;
584         default:
585                 break;
586         }
587
588 #ifdef WITH_DYNAMIC_CLIENTS
589         if (c->client_server) {
590                 free(c->secret);
591                 c->secret = strdup("testing123");
592
593                 if (((c->ipaddr.af == AF_INET) &&
594                      (c->prefix == 32)) ||
595                     ((c->ipaddr.af == AF_INET6) &&
596                      (c->prefix == 128))) {
597                         cf_log_err(cf_sectiontoitem(cs),
598                                    "Dynamic clients MUST be a network, not a single IP address.");
599                         client_free(c);
600                         return NULL;
601                 }
602
603                 return c;
604         }
605 #endif
606
607         if (!c->secret || !*c->secret) {
608 #ifdef WITH_DHCP
609                 const char *value = NULL;
610                 CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
611
612                 if (cp) value = cf_pair_value(cp);
613
614                 /*
615                  *      Secrets aren't needed for DHCP.
616                  */
617                 if (value && (strcmp(value, "yes") == 0)) return c;
618
619 #endif
620                 client_free(c);
621                 cf_log_err(cf_sectiontoitem(cs),
622                            "secret must be at least 1 character long");
623                 return NULL;
624         }
625
626
627         return c;
628 }
629
630 /*
631  *      Create the linked list of clients from the new configuration
632  *      type.  This way we don't have to change too much in the other
633  *      source-files.
634  */
635 RADCLIENT_LIST *clients_parse_section(CONF_SECTION *section)
636 {
637         int             global = FALSE, in_server = FALSE;
638         CONF_SECTION    *cs;
639         RADCLIENT       *c;
640         RADCLIENT_LIST  *clients;
641
642         /*
643          *      Be forgiving.  If there's already a clients, return
644          *      it.  Otherwise create a new one.
645          */
646         clients = cf_data_find(section, "clients");
647         if (clients) return clients;
648
649         clients = clients_init();
650         if (!clients) return NULL;
651
652         if (cf_top_section(section) == section) global = TRUE;
653
654         if (strcmp("server", cf_section_name1(section)) == 0) in_server = TRUE;
655
656         /*
657          *      Associate the clients structure with the section, where
658          *      it will be freed once the section is freed.
659          */
660         if (cf_data_add(section, "clients", clients, (void *) clients_free) < 0) {
661                 cf_log_err(cf_sectiontoitem(section),
662                            "Failed to associate clients with section %s",
663                        cf_section_name1(section));
664                 clients_free(clients);
665                 return NULL;
666         }
667
668         for (cs = cf_subsection_find_next(section, NULL, "client");
669              cs != NULL;
670              cs = cf_subsection_find_next(section, cs, "client")) {
671                 c = client_parse(cs, in_server);
672                 if (!c) {
673                         return NULL;
674                 }
675
676                 /*
677                  *      FIXME: Add the client as data via cf_data_add,
678                  *      for migration issues.
679                  */
680
681                 if (!client_add(clients, c)) {
682                         cf_log_err(cf_sectiontoitem(cs),
683                                    "Failed to add client %s",
684                                    cf_section_name2(cs));
685                         client_free(c);
686                         return NULL;
687                 }
688         }
689
690         /*
691          *      Replace the global list of clients with the new one.
692          *      The old one is still referenced from the original
693          *      configuration, and will be freed when that is freed.
694          */
695         if (global) {
696                 root_clients = clients;
697         }
698
699         return clients;
700 }
701
702 #ifdef WITH_DYNAMIC_CLIENTS
703 /*
704  *      We overload this structure a lot.
705  */
706 static const CONF_PARSER dynamic_config[] = {
707         { "FreeRADIUS-Client-IP-Address",  PW_TYPE_IPADDR,
708           offsetof(RADCLIENT, ipaddr), 0, NULL },
709         { "FreeRADIUS-Client-IPv6-Address",  PW_TYPE_IPV6ADDR,
710           offsetof(RADCLIENT, ipaddr), 0, NULL },
711
712         { "FreeRADIUS-Client-Require-MA",  PW_TYPE_BOOLEAN,
713           offsetof(RADCLIENT, message_authenticator), NULL, NULL },
714
715         { "FreeRADIUS-Client-Secret",  PW_TYPE_STRING_PTR,
716           offsetof(RADCLIENT, secret), 0, "" },
717         { "FreeRADIUS-Client-Shortname",  PW_TYPE_STRING_PTR,
718           offsetof(RADCLIENT, shortname), 0, "" },
719         { "FreeRADIUS-Client-NAS-Type",  PW_TYPE_STRING_PTR,
720           offsetof(RADCLIENT, nastype), 0, NULL },
721         { "FreeRADIUS-Client-Virtual-Server",  PW_TYPE_STRING_PTR,
722           offsetof(RADCLIENT, server), 0, NULL },
723
724         { NULL, -1, 0, NULL, NULL }
725 };
726
727 RADCLIENT *client_create(RADCLIENT_LIST *clients, REQUEST *request)
728 {
729         int i, *pi;
730         char **p;
731         RADCLIENT *c;
732         char buffer[128];
733
734         if (!clients || !request) return NULL;
735
736         c = rad_malloc(sizeof(*c));
737         memset(c, 0, sizeof(*c));
738         c->cs = request->client->cs;
739         c->ipaddr.af = AF_UNSPEC;
740
741         for (i = 0; dynamic_config[i].name != NULL; i++) {
742                 DICT_ATTR *da;
743                 VALUE_PAIR *vp;
744
745                 da = dict_attrbyname(dynamic_config[i].name);
746                 if (!da) {
747                         DEBUG("- Cannot add client %s: attribute \"%s\"is not in the dictionary",
748                               ip_ntoh(&request->packet->src_ipaddr,
749                                       buffer, sizeof(buffer)),
750                               dynamic_config[i].name);
751                 error:
752                         client_free(c);
753                         return NULL;
754                 }
755
756                 vp = pairfind(request->config_items, da->attr);
757                 if (!vp) {
758                         /*
759                          *      Not required.  Skip it.
760                          */
761                         if (!dynamic_config[i].dflt) continue;
762                         
763                         DEBUG("- Cannot add client %s: Required attribute \"%s\" is missing.",  
764                               ip_ntoh(&request->packet->src_ipaddr,
765                                       buffer, sizeof(buffer)),
766                               dynamic_config[i].name);
767                         goto error;
768                 }
769
770                 switch (dynamic_config[i].type) {
771                 case PW_TYPE_IPADDR:
772                         c->ipaddr.af = AF_INET;
773                         c->ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
774                         c->prefix = 32;
775                         break;
776
777                 case PW_TYPE_IPV6ADDR:
778                         c->ipaddr.af = AF_INET6;
779                         c->ipaddr.ipaddr.ip6addr = vp->vp_ipv6addr;
780                         c->prefix = 128;
781                         break;
782
783                 case PW_TYPE_STRING_PTR:
784                         p = (char **) ((char *) c + dynamic_config[i].offset);
785                         if (*p) free(*p);
786                         *p = strdup(vp->vp_strvalue);
787                         break;
788
789                 case PW_TYPE_BOOLEAN:
790                         pi = (int *) ((char *) c + dynamic_config[i].offset);
791                         *pi = vp->vp_integer;
792                         break;
793
794                 default:
795                         goto error;
796                 }
797         }
798
799         if (c->ipaddr.af == AF_UNSPEC) {
800                 DEBUG("- Cannot add client %s: No IP address was specified.",
801                       ip_ntoh(&request->packet->src_ipaddr,
802                               buffer, sizeof(buffer)));
803
804                 goto error;
805         }
806
807         if (fr_ipaddr_cmp(&request->packet->src_ipaddr, &c->ipaddr) != 0) {
808                 char buf2[128];
809
810                 DEBUG("- Cannot add client %s: IP address %s do not match",
811                       ip_ntoh(&request->packet->src_ipaddr,
812                               buffer, sizeof(buffer)),
813                       ip_ntoh(&c->ipaddr,
814                               buf2, sizeof(buf2)));                   
815                 goto error;
816         }
817
818         /*
819          *      No virtual server defined.  Inherit the parent's
820          *      definition.
821          */
822         if (request->client->server && !c->server) {
823                 c->server = strdup(request->client->server);
824         }
825
826         /*
827          *      If the client network isn't global (not tied to a
828          *      virtual server), then ensure that this clients server
829          *      is the same as the enclosing networks virtual server.
830          */
831         if (request->client->server &&
832              (strcmp(request->client->server, c->server) != 0)) {
833                 DEBUG("- Cannot add client %s: Virtual server %s is not the same as the virtual server for the network.",
834                       ip_ntoh(&request->packet->src_ipaddr,
835                               buffer, sizeof(buffer)),
836                       c->server);
837
838                 goto error;
839         }
840
841         if (!client_add(clients, c)) {
842                 DEBUG("- Cannot add client %s: Internal error",
843                       ip_ntoh(&request->packet->src_ipaddr,
844                               buffer, sizeof(buffer)));
845
846                 goto error;
847         }
848
849         /*
850          *      Initialize the remaining fields.
851          */
852         c->dynamic = TRUE;
853         c->lifetime = request->client->lifetime;
854         c->created = request->timestamp;
855         c->longname = strdup(c->shortname);
856
857         DEBUG("- Added client %s with shared secret %s",
858               ip_ntoh(&request->packet->src_ipaddr, buffer, sizeof(buffer)),
859               c->secret);
860
861         return c;
862 }
863
864 #endif