/
conffile.c
3155 lines (2581 loc) · 63.8 KB
/
conffile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* conffile.c Read the radiusd.conf file.
*
* Yep I should learn to use lex & yacc, or at least
* write a decent parser. I know how to do that, really :)
* miquels@cistron.nl
*
* Version: $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
* Copyright 2000,2006 The FreeRADIUS server project
* Copyright 2000 Miquel van Smoorenburg <miquels@cistron.nl>
* Copyright 2000 Alan DeKok <aland@ox.org>
*/
RCSID("$Id$")
#include <freeradius-devel/radiusd.h>
#include <freeradius-devel/parser.h>
#include <freeradius-devel/rad_assert.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <ctype.h>
typedef enum conf_property {
CONF_PROPERTY_INVALID = 0,
CONF_PROPERTY_NAME,
CONF_PROPERTY_INSTANCE,
} CONF_PROPERTY;
const FR_NAME_NUMBER conf_property_name[] = {
{ "name", CONF_PROPERTY_NAME},
{ "instance", CONF_PROPERTY_INSTANCE},
{ NULL , -1 }
};
typedef enum conf_type {
CONF_ITEM_INVALID = 0,
CONF_ITEM_PAIR,
CONF_ITEM_SECTION,
CONF_ITEM_DATA
} CONF_ITEM_TYPE;
struct conf_item {
struct conf_item *next;
struct conf_part *parent;
int lineno;
char const *filename;
CONF_ITEM_TYPE type;
};
struct conf_pair {
CONF_ITEM item;
char const *attr;
char const *value;
FR_TOKEN op;
FR_TOKEN value_type;
};
struct conf_part {
CONF_ITEM item;
char const *name1;
char const *name2;
struct conf_item *children;
struct conf_item *tail; /* for speed */
CONF_SECTION *template;
rbtree_t *pair_tree; /* and a partridge.. */
rbtree_t *section_tree; /* no jokes here */
rbtree_t *name2_tree; /* for sections of the same name2 */
rbtree_t *data_tree;
void *base;
int depth;
CONF_PARSER const *variables;
};
CONF_SECTION *root_config = NULL;
/*
* Internal data that is associated with a configuration section,
* so that we don't have to track it separately.
*/
struct conf_data {
CONF_ITEM item;
char const *name;
int flag;
void *data; /* user data */
void (*free)(void *); /* free user data function */
};
static int cf_data_add_internal(CONF_SECTION *cs, char const *name,
void *data, void (*data_free)(void *),
int flag);
static void *cf_data_find_internal(CONF_SECTION const *cs, char const *name,
int flag);
static char const *cf_expand_variables(char const *cf, int *lineno,
CONF_SECTION *outercs,
char *output, size_t outsize,
char const *input);
/*
* Isolate the scary casts in these tiny provably-safe functions
*/
CONF_PAIR *cf_itemtopair(CONF_ITEM const *ci)
{
CONF_PAIR *out;
if (ci == NULL) {
return NULL;
}
rad_assert(ci->type == CONF_ITEM_PAIR);
memcpy(&out, &ci, sizeof(out));
return out;
}
CONF_SECTION *cf_itemtosection(CONF_ITEM const *ci)
{
CONF_SECTION *out;
if (ci == NULL) {
return NULL;
}
rad_assert(ci->type == CONF_ITEM_SECTION);
memcpy(&out, &ci, sizeof(out));
return out;
}
CONF_ITEM *cf_pairtoitem(CONF_PAIR const *cp)
{
CONF_ITEM *out;
if (cp == NULL) {
return NULL;
}
memcpy(&out, &cp, sizeof(out));
return out;
}
CONF_ITEM *cf_sectiontoitem(CONF_SECTION const *cs)
{
CONF_ITEM *out;
if (cs == NULL) {
return NULL;
}
memcpy(&out, &cs, sizeof(out));
return out;
}
static CONF_ITEM *cf_datatoitem(CONF_DATA const *cd)
{
CONF_ITEM *out;
if (cd == NULL) {
return NULL;
}
memcpy(&out, &cd, sizeof(out));
return out;
}
/*
* Create a new CONF_PAIR
*/
static CONF_PAIR *cf_pair_alloc(CONF_SECTION *parent, char const *attr,
char const *value, FR_TOKEN op,
FR_TOKEN value_type)
{
CONF_PAIR *cp;
if (!attr) return NULL;
cp = talloc_zero(parent, CONF_PAIR);
if (!cp) return NULL;
cp->item.type = CONF_ITEM_PAIR;
cp->item.parent = parent;
cp->value_type = value_type;
cp->op = op;
cp->attr = talloc_strdup(cp, attr);
if (!cp->attr) {
error:
talloc_free(cp);
return NULL;
}
if (value) {
cp->value = talloc_strdup(cp, value);
if (!cp->value) goto error;
}
return cp;
}
static int cf_data_free(void *ctx)
{
CONF_DATA *cd;
cd = talloc_get_type_abort(ctx, CONF_DATA);
if (cd->free) {
cd->free(cd->data);
}
return 0;
}
/*
* rbtree callback function
*/
static int pair_cmp(void const *a, void const *b)
{
CONF_PAIR const *one = a;
CONF_PAIR const *two = b;
return strcmp(one->attr, two->attr);
}
/*
* rbtree callback function
*/
static int section_cmp(void const *a, void const *b)
{
CONF_SECTION const *one = a;
CONF_SECTION const *two = b;
return strcmp(one->name1, two->name1);
}
/*
* rbtree callback function
*/
static int name2_cmp(void const *a, void const *b)
{
CONF_SECTION const *one = a;
CONF_SECTION const *two = b;
rad_assert(strcmp(one->name1, two->name1) == 0);
if (!one->name2 && !two->name2) return 0;
if (!one->name2) return -1;
if (!two->name2) return +1;
return strcmp(one->name2, two->name2);
}
/*
* rbtree callback function
*/
static int data_cmp(void const *a, void const *b)
{
int rcode;
CONF_DATA const *one = a;
CONF_DATA const *two = b;
rcode = one->flag - two->flag;
if (rcode != 0) return rcode;
return strcmp(one->name, two->name);
}
static int cf_section_free(void *ctx)
{
CONF_SECTION *cs;
cs = talloc_get_type_abort(ctx, CONF_SECTION);
/*
* Name1 and name2 are allocated contiguous with
* cs.
*/
if (cs->pair_tree) {
rbtree_free(cs->pair_tree);
cs->pair_tree = NULL;
}
if (cs->section_tree) {
rbtree_free(cs->section_tree);
cs->section_tree = NULL;
}
if (cs->name2_tree) {
rbtree_free(cs->name2_tree);
cs->name2_tree = NULL;
}
if (cs->data_tree) {
rbtree_free(cs->data_tree);
cs->data_tree = NULL;
}
return 0;
}
/*
* Allocate a CONF_SECTION
*/
CONF_SECTION *cf_section_alloc(CONF_SECTION *parent, char const *name1,
char const *name2)
{
CONF_SECTION *cs;
char buffer[1024];
if (!name1) return NULL;
if (name2) {
if (strchr(name2, '$')) {
name2 = cf_expand_variables(parent->item.filename,
&parent->item.lineno,
parent,
buffer, sizeof(buffer), name2);
if (!name2) {
ERROR("Failed expanding section name");
return NULL;
}
}
}
cs = talloc_zero(parent, CONF_SECTION);
if (!cs) return NULL;
cs->item.type = CONF_ITEM_SECTION;
cs->item.parent = parent;
cs->name1 = talloc_strdup(cs, name1);
if (!cs->name1) {
error:
talloc_free(cs);
return NULL;
}
if (name2 && *name2) {
cs->name2 = talloc_strdup(cs, name2);
if (!cs->name2) goto error;
}
cs->pair_tree = rbtree_create(pair_cmp, NULL, 0);
if (!cs->pair_tree) goto error;
talloc_set_destructor((void *) cs, cf_section_free);
/*
* Don't create a data tree, it may not be needed.
*/
/*
* Don't create the section tree here, it may not
* be needed.
*/
if (parent) cs->depth = parent->depth + 1;
return cs;
}
/*
* Replace pair in a given section with a new pair,
* of the given value.
*/
int cf_pair_replace(CONF_SECTION *cs, CONF_PAIR *cp, char const *value)
{
CONF_PAIR *newp;
CONF_ITEM *ci, *cn, **last;
newp = cf_pair_alloc(cs, cp->attr, value, cp->op, cp->value_type);
if (!newp) return -1;
ci = &(cp->item);
cn = &(newp->item);
/*
* Find the old one from the linked list, and replace it
* with the new one.
*/
for (last = &cs->children; (*last) != NULL; last = &(*last)->next) {
if (*last == ci) {
cn->next = (*last)->next;
*last = cn;
ci->next = NULL;
break;
}
}
rbtree_deletebydata(cs->pair_tree, ci);
rbtree_insert(cs->pair_tree, cn);
return 0;
}
/*
* Add an item to a configuration section.
*/
static void cf_item_add(CONF_SECTION *cs, CONF_ITEM *ci)
{
if (!cs || !ci) return;
if (!cs->children) {
rad_assert(cs->tail == NULL);
cs->children = ci;
} else {
rad_assert(cs->tail != NULL);
cs->tail->next = ci;
}
/*
* Update the trees (and tail) for each item added.
*/
for (/* nothing */; ci != NULL; ci = ci->next) {
cs->tail = ci;
/*
* For fast lookups, pairs and sections get
* added to rbtree's.
*/
switch (ci->type) {
case CONF_ITEM_PAIR:
rbtree_insert(cs->pair_tree, ci);
break;
case CONF_ITEM_SECTION: {
CONF_SECTION *cs_new = cf_itemtosection(ci);
CONF_SECTION *name1_cs;
if (!cs->section_tree) {
cs->section_tree = rbtree_create(section_cmp, NULL, 0);
if (!cs->section_tree) {
ERROR("Out of memory");
fr_exit_now(1);
}
}
name1_cs = rbtree_finddata(cs->section_tree, cs_new);
if (!name1_cs) {
if (!rbtree_insert(cs->section_tree, cs_new)) {
ERROR("Failed inserting section into tree");
fr_exit_now(1);
}
}
/*
* We already have a section of
* this "name1". Add a new
* sub-section based on name2.
*/
if (name1_cs) {
if (!name1_cs->name2_tree) {
name1_cs->name2_tree = rbtree_create(name2_cmp,
NULL, 0);
if (!name1_cs->name2_tree) {
ERROR("Out of memory");
fr_exit_now(1);
}
}
/*
* We don't care if this
* fails. If the user
* tries to create two
* sections of the same
* name1/name2, the
* duplicate section is
* just silently ignored.
*/
rbtree_insert(name1_cs->name2_tree, cs_new);
}
break;
} /* was a section */
case CONF_ITEM_DATA:
if (!cs->data_tree) {
cs->data_tree = rbtree_create(data_cmp, NULL, 0);
}
if (cs->data_tree) {
rbtree_insert(cs->data_tree, ci);
}
break;
default: /* FIXME: assert & error! */
break;
} /* switch over conf types */
} /* loop over ci */
}
CONF_ITEM *cf_reference_item(CONF_SECTION const *parentcs,
CONF_SECTION *outercs,
char const *ptr)
{
CONF_PAIR *cp;
CONF_SECTION *next;
CONF_SECTION const *cs = outercs;
char name[8192];
char *p;
strlcpy(name, ptr, sizeof(name));
p = name;
/*
* ".foo" means "foo from the current section"
*/
if (*p == '.') {
p++;
/*
* Just '.' means the current section
*/
if (*p == '\0') {
return cf_sectiontoitem(cs);
}
/*
* ..foo means "foo from the section
* enclosing this section" (etc.)
*/
while (*p == '.') {
if (cs->item.parent) {
cs = cs->item.parent;
}
/*
* .. means the section
* enclosing this section
*/
if (!*++p) {
return cf_sectiontoitem(cs);
}
}
/*
* "foo.bar.baz" means "from the root"
*/
} else if (strchr(p, '.') != NULL) {
if (!parentcs) goto no_such_item;
cs = parentcs;
}
while (*p) {
char *q, *r;
r = strchr(p, '[');
q = strchr(p, '.');
if (!r && !q) break;
if (r && q > r) q = NULL;
if (q && q < r) r = NULL;
/*
* Split off name2.
*/
if (r) {
q = strchr(r + 1, ']');
if (!q) return NULL; /* parse error */
/*
* Points to foo[bar]xx: parse error,
* it should be foo[bar] or foo[bar].baz
*/
if (q[1] && q[1] != '.') goto no_such_item;
*r = '\0';
*q = '\0';
next = cf_section_sub_find_name2(cs, p, r + 1);
*r = '[';
*q = ']';
/*
* Points to a named instance of a section.
*/
if (!q[1]) {
if (!next) goto no_such_item;
return &(next->item);
}
q++; /* ensure we skip the ']' and '.' */
} else {
*q = '\0';
next = cf_section_sub_find(cs, p);
*q = '.';
}
if (!next) break; /* it MAY be a pair in this section! */
cs = next;
p = q + 1;
}
if (!*p) goto no_such_item;
retry:
/*
* Find it in the current referenced
* section.
*/
cp = cf_pair_find(cs, p);
if (cp) return &(cp->item);
next = cf_section_sub_find(cs, p);
if (next) return &(next->item);
/*
* "foo" is "in the current section, OR in main".
*/
if ((p == name) && (parentcs != NULL) && (cs != parentcs)) {
cs = parentcs;
goto retry;
}
no_such_item:
WDEBUG2("No such configuration item %s", ptr);
return NULL;
}
CONF_SECTION *cf_top_section(CONF_SECTION *cs)
{
if (!cs) return NULL;
while (cs->item.parent != NULL) {
cs = cs->item.parent;
}
return cs;
}
/*
* Expand the variables in an input string.
*/
static char const *cf_expand_variables(char const *cf, int *lineno,
CONF_SECTION *outercs,
char *output, size_t outsize,
char const *input)
{
char *p;
char const *end, *ptr;
CONF_SECTION const *parentcs;
char name[8192];
/*
* Find the master parent conf section.
* We can't use mainconfig.config, because we're in the
* process of re-building it, and it isn't set up yet...
*/
parentcs = cf_top_section(outercs);
p = output;
ptr = input;
while (*ptr) {
/*
* Ignore anything other than "${"
*/
if ((*ptr == '$') && (ptr[1] == '{')) {
CONF_ITEM *ci;
CONF_PAIR *cp;
char *q;
/*
* FIXME: Add support for ${foo:-bar},
* like in xlat.c
*/
/*
* Look for trailing '}', and log a
* warning for anything that doesn't match,
* and exit with a fatal error.
*/
end = strchr(ptr, '}');
if (end == NULL) {
*p = '\0';
INFO("%s[%d]: Variable expansion missing }",
cf, *lineno);
return NULL;
}
ptr += 2;
/*
* Can't really happen because input lines are
* capped at 8k, which is sizeof(name)
*/
if ((size_t) (end - ptr) >= sizeof(name)) {
ERROR("%s[%d]: Reference string is too large",
cf, *lineno);
return NULL;
}
memcpy(name, ptr, end - ptr);
name[end - ptr] = '\0';
q = strchr(name, ':');
if (q) {
*(q++) = '\0';
}
ci = cf_reference_item(parentcs, outercs, name);
if (!ci) {
ERROR("%s[%d]: Reference \"%s\" not found", cf, *lineno, input);
return NULL;
}
/*
* The expansion doesn't refer to another item or section
* it's the property of a section.
*/
if (q) {
CONF_SECTION *mycs = cf_itemtosection(ci);
if (ci->type != CONF_ITEM_SECTION) {
ERROR("%s[%d]: Can only reference properties of sections", cf, *lineno);
return NULL;
}
switch (fr_str2int(conf_property_name, q, CONF_PROPERTY_INVALID)) {
case CONF_PROPERTY_NAME:
strcpy(p, mycs->name1);
break;
case CONF_PROPERTY_INSTANCE:
strcpy(p, mycs->name2 ? mycs->name2 : mycs->name1);
break;
default:
ERROR("%s[%d]: Invalid property '%s'", cf, *lineno, q);
return NULL;
}
p += strlen(p);
ptr = end + 1;
} else if (ci->type == CONF_ITEM_PAIR) {
/*
* Substitute the value of the variable.
*/
cp = cf_itemtopair(ci);
if (!cp->value) {
ERROR("%s[%d]: Reference \"%s\" has no value",
cf, *lineno, input);
return NULL;
}
if (p + strlen(cp->value) >= output + outsize) {
ERROR("%s[%d]: Reference \"%s\" is too long",
cf, *lineno, input);
return NULL;
}
strcpy(p, cp->value);
p += strlen(p);
ptr = end + 1;
} else if (ci->type == CONF_ITEM_SECTION) {
/*
* Adding an entry again to a
* section is wrong. We don't
* want an infinite loop.
*/
if (ci->parent == outercs) {
ERROR("%s[%d]: Cannot reference different item in same section", cf, *lineno);
return NULL;
}
cf_item_add(outercs, ci);
(void) talloc_reference(outercs, ci);
ptr = end + 1;
} else {
ERROR("%s[%d]: Reference \"%s\" type is invalid", cf, *lineno, input);
return NULL;
}
} else if (memcmp(ptr, "$ENV{", 5) == 0) {
char *env;
ptr += 5;
/*
* Look for trailing '}', and log a
* warning for anything that doesn't match,
* and exit with a fatal error.
*/
end = strchr(ptr, '}');
if (end == NULL) {
*p = '\0';
INFO("%s[%d]: Environment variable expansion missing }",
cf, *lineno);
return NULL;
}
/*
* Can't really happen because input lines are
* capped at 8k, which is sizeof(name)
*/
if ((size_t) (end - ptr) >= sizeof(name)) {
ERROR("%s[%d]: Environment variable name is too large",
cf, *lineno);
return NULL;
}
memcpy(name, ptr, end - ptr);
name[end - ptr] = '\0';
/*
* Get the environment variable.
* If none exists, then make it an empty string.
*/
env = getenv(name);
if (env == NULL) {
*name = '\0';
env = name;
}
if (p + strlen(env) >= output + outsize) {
ERROR("%s[%d]: Reference \"%s\" is too long",
cf, *lineno, input);
return NULL;
}
strcpy(p, env);
p += strlen(p);
ptr = end + 1;
} else {
/*
* Copy it over verbatim.
*/
*(p++) = *(ptr++);
}
if (p >= (output + outsize)) {
ERROR("%s[%d]: Reference \"%s\" is too long",
cf, *lineno, input);
return NULL;
}
} /* loop over all of the input string. */
*p = '\0';
return output;
}
static char const *parse_spaces = " ";
/*
* Parses an item (not a CONF_ITEM) into the specified format,
* with a default value.
*
* Returns -1 on error, -2 if deprecated, 0 for correctly parsed,
* and 1 if the default value was used. Note that the default
* value will be used ONLY if the CONF_PAIR is NULL.
*/
int cf_item_parse(CONF_SECTION *cs, char const *name, int type, void *data, char const *dflt)
{
int rcode;
bool deprecated, required, attribute, secret;
char **q;
char const *value;
fr_ipaddr_t ipaddr;
CONF_PAIR const *cp = NULL;
char ipbuf[128];
if (!cs) return -1;
deprecated = (type & PW_TYPE_DEPRECATED);
required = (type & PW_TYPE_REQUIRED);
attribute = (type & PW_TYPE_ATTRIBUTE);
secret = (type & PW_TYPE_SECRET);
type &= 0xff; /* normal types are small */
rcode = 0;
if (attribute) {
required = 1;
}
cp = cf_pair_find(cs, name);
if (cp) {
value = cp->value;
} else {
rcode = 1;
value = dflt;
}
if (!value) {
if (required) {
cf_log_err(&(cs->item), "Configuration item '%s' must have a value", name);
return -1;
}
return rcode;
}
if (!*value && required) {
is_required:
if (!cp) {
cf_log_err(&(cs->item), "Configuration item '%s' must not be empty", name);
} else {
cf_log_err(&(cp->item), "Configuration item '%s' must not be empty", name);
}
return -1;
}
if (deprecated) {
cf_log_err(&(cs->item), "Configuration item \"%s\" is deprecated", name);
return -2;
}
switch (type) {
case PW_TYPE_BOOLEAN:
/*
* Allow yes/no and on/off
*/
if ((strcasecmp(value, "yes") == 0) ||
(strcasecmp(value, "on") == 0)) {
*(bool *)data = true;
} else if ((strcasecmp(value, "no") == 0) ||
(strcasecmp(value, "off") == 0)) {
*(bool *)data = false;
} else {
*(bool *)data = false;
cf_log_err(&(cs->item), "Invalid value \"%s\" for boolean "
"variable %s", value, name);
return -1;
}
cf_log_info(cs, "%.*s\t%s = %s",
cs->depth, parse_spaces, name, value);
break;
case PW_TYPE_INTEGER:
*(int *)data = strtol(value, 0, 0);
cf_log_info(cs, "%.*s\t%s = %d",
cs->depth, parse_spaces, name, *(int *)data);
break;
case PW_TYPE_STRING_PTR:
q = (char **) data;
if (*q != NULL) {
talloc_free(*q);
}
/*
* Expand variables which haven't already been
* expanded automagically when the configuration
* file was read.
*/
if (value == dflt) {
char buffer[8192];
int lineno = 0;
lineno = cs->item.lineno;
/*
* FIXME: sizeof(buffer)?
*/
value = cf_expand_variables("<internal>",
&lineno,
cs, buffer, sizeof(buffer),
value);
if (!value) {
cf_log_err(&(cs->item),"Failed expanding variable %s", name);
return -1;
}
}
if (required && (!value || !*value)) goto is_required;
if (attribute) {
if (!dict_attrbyname(value)) {
if (!cp) {
cf_log_err(&(cs->item), "No such attribute '%s' for configuration '%s'",
value, name);
} else {
cf_log_err(&(cp->item), "No such attribute '%s'",