-
Notifications
You must be signed in to change notification settings - Fork 271
/
settings-parser.c
2117 lines (1850 loc) · 53.9 KB
/
settings-parser.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
/* Copyright (c) 2002-2016 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "hash.h"
#include "net.h"
#include "istream.h"
#include "env-util.h"
#include "execv-const.h"
#include "str.h"
#include "strescape.h"
#include "var-expand.h"
#include "settings-parser.h"
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define IS_WHITE(c) ((c) == ' ' || (c) == '\t')
struct setting_link {
struct setting_link *parent;
const struct setting_parser_info *info;
const char *full_key;
/* Points to array inside parent->set_struct.
SET_DEFLIST : array of set_structs
SET_STRLIST : array of const_strings */
ARRAY_TYPE(void_array) *array;
/* Pointer to structure containing the values */
void *set_struct;
/* Pointer to structure containing non-zero values for settings that
have been changed. */
void *change_struct;
/* SET_DEFLIST: array of change_structs */
ARRAY_TYPE(void_array) *change_array;
};
struct setting_parser_context {
pool_t set_pool, parser_pool;
enum settings_parser_flags flags;
bool str_vars_are_expanded;
struct setting_link *roots;
unsigned int root_count;
HASH_TABLE(char *, struct setting_link *) links;
unsigned int linenum;
const char *error;
const struct setting_parser_info *prev_info;
};
static const struct setting_parser_info strlist_info = {
.module_name = NULL,
.defines = NULL,
.defaults = NULL,
.type_offset = (size_t)-1,
.struct_size = 0,
.parent_offset = (size_t)-1
};
HASH_TABLE_DEFINE_TYPE(setting_link, struct setting_link *,
struct setting_link *);
static int settings_parse_keyvalue(struct setting_parser_context *ctx,
const char *key, const char *value);
static void
setting_parser_copy_defaults(struct setting_parser_context *ctx,
const struct setting_parser_info *info,
struct setting_link *link);
static int
settings_apply(struct setting_link *dest_link,
const struct setting_link *src_link,
pool_t pool, const char **conflict_key_r);
struct setting_parser_context *
settings_parser_init(pool_t set_pool, const struct setting_parser_info *root,
enum settings_parser_flags flags)
{
return settings_parser_init_list(set_pool, &root, 1, flags);
}
static void
copy_unique_defaults(struct setting_parser_context *ctx,
const struct setting_define *def,
struct setting_link *link)
{
ARRAY_TYPE(void_array) *arr =
STRUCT_MEMBER_P(link->set_struct, def->offset);
ARRAY_TYPE(void_array) *carr = NULL;
struct setting_link *new_link;
struct setting_parser_info info;
const char *const *keyp, *key, *prefix;
void *const *children;
void *new_set, *new_changes = NULL;
char *full_key;
unsigned int i, count;
if (!array_is_created(arr))
return;
children = array_get(arr, &count);
if (link->change_struct != NULL) {
carr = STRUCT_MEMBER_P(link->change_struct, def->offset);
i_assert(!array_is_created(carr));
p_array_init(carr, ctx->set_pool, count + 4);
}
p_array_init(arr, ctx->set_pool, count + 4);
memset(&info, 0, sizeof(info));
info = *def->list_info;
for (i = 0; i < count; i++) T_BEGIN {
new_set = p_malloc(ctx->set_pool, info.struct_size);
array_append(arr, &new_set, 1);
if (link->change_struct != NULL) {
new_changes = p_malloc(ctx->set_pool, info.struct_size);
array_append(carr, &new_changes, 1);
}
keyp = CONST_PTR_OFFSET(children[i], info.type_offset);
key = settings_section_escape(*keyp);
new_link = p_new(ctx->set_pool, struct setting_link, 1);
prefix = link->full_key == NULL ?
t_strconcat(def->key, SETTINGS_SEPARATOR_S, NULL) :
t_strconcat(link->full_key, SETTINGS_SEPARATOR_S,
def->key, SETTINGS_SEPARATOR_S,NULL);
full_key = p_strconcat(ctx->set_pool, prefix, key, NULL);
new_link->full_key = full_key;
new_link->parent = link;
new_link->info = def->list_info;
new_link->array = arr;
new_link->change_array = carr;
new_link->set_struct = new_set;
new_link->change_struct = new_changes;
i_assert(hash_table_lookup(ctx->links, full_key) == NULL);
hash_table_insert(ctx->links, full_key, new_link);
info.defaults = children[i];
setting_parser_copy_defaults(ctx, &info, new_link);
} T_END;
}
static void
setting_parser_copy_defaults(struct setting_parser_context *ctx,
const struct setting_parser_info *info,
struct setting_link *link)
{
const struct setting_define *def;
const char *p, **strp;
if (info->defaults == NULL)
return;
memcpy(link->set_struct, info->defaults, info->struct_size);
for (def = info->defines; def->key != NULL; def++) {
switch (def->type) {
case SET_ENUM: {
/* fix enums by dropping everything after the
first ':' */
strp = STRUCT_MEMBER_P(link->set_struct, def->offset);
p = strchr(*strp, ':');
if (p != NULL)
*strp = p_strdup_until(ctx->set_pool, *strp, p);
break;
}
case SET_STR_VARS: {
/* insert the unexpanded-character */
strp = STRUCT_MEMBER_P(link->set_struct, def->offset);
if (*strp != NULL) {
*strp = p_strconcat(ctx->set_pool,
SETTING_STRVAR_UNEXPANDED,
*strp, NULL);
}
break;
}
case SET_DEFLIST_UNIQUE:
copy_unique_defaults(ctx, def, link);
break;
default:
break;
}
}
}
struct setting_parser_context *
settings_parser_init_list(pool_t set_pool,
const struct setting_parser_info *const *roots,
unsigned int count, enum settings_parser_flags flags)
{
struct setting_parser_context *ctx;
unsigned int i;
pool_t parser_pool;
i_assert(count > 0);
parser_pool = pool_alloconly_create(MEMPOOL_GROWING"settings parser",
1024);
ctx = p_new(parser_pool, struct setting_parser_context, 1);
ctx->set_pool = set_pool;
ctx->parser_pool = parser_pool;
ctx->flags = flags;
/* use case-insensitive comparisons. this is mainly because settings
may go through environment variables where their keys get
uppercased. of course the alternative would be to not uppercase
environment. probably doesn't make much difference which way is
chosen. */
hash_table_create(&ctx->links, ctx->parser_pool, 0,
strcase_hash, strcasecmp);
ctx->root_count = count;
ctx->roots = p_new(ctx->parser_pool, struct setting_link, count);
for (i = 0; i < count; i++) {
ctx->roots[i].info = roots[i];
if (roots[i]->struct_size == 0)
continue;
ctx->roots[i].set_struct =
p_malloc(ctx->set_pool, roots[i]->struct_size);
if ((flags & SETTINGS_PARSER_FLAG_TRACK_CHANGES) != 0) {
ctx->roots[i].change_struct =
p_malloc(ctx->set_pool, roots[i]->struct_size);
}
setting_parser_copy_defaults(ctx, roots[i], &ctx->roots[i]);
}
pool_ref(ctx->set_pool);
return ctx;
}
void settings_parser_deinit(struct setting_parser_context **_ctx)
{
struct setting_parser_context *ctx = *_ctx;
*_ctx = NULL;
hash_table_destroy(&ctx->links);
pool_unref(&ctx->set_pool);
pool_unref(&ctx->parser_pool);
}
void *settings_parser_get(struct setting_parser_context *ctx)
{
i_assert(ctx->root_count == 1);
return ctx->roots[0].set_struct;
}
void **settings_parser_get_list(const struct setting_parser_context *ctx)
{
unsigned int i;
void **sets;
sets = t_new(void *, ctx->root_count + 1);
for (i = 0; i < ctx->root_count; i++)
sets[i] = ctx->roots[i].set_struct;
return sets;
}
void *settings_parser_get_changes(struct setting_parser_context *ctx)
{
i_assert(ctx->root_count == 1);
return ctx->roots[0].change_struct;
}
const struct setting_parser_info *const *
settings_parser_get_roots(const struct setting_parser_context *ctx)
{
const struct setting_parser_info **infos;
unsigned int i;
infos = t_new(const struct setting_parser_info *, ctx->root_count + 1);
for (i = 0; i < ctx->root_count; i++)
infos[i] = ctx->roots[i].info;
return infos;
}
const char *settings_parser_get_error(struct setting_parser_context *ctx)
{
return ctx->error;
}
static const struct setting_define *
setting_define_find(const struct setting_parser_info *info, const char *key)
{
const struct setting_define *list;
for (list = info->defines; list->key != NULL; list++) {
if (strcmp(list->key, key) == 0)
return list;
}
return NULL;
}
static int
get_bool(struct setting_parser_context *ctx, const char *value, bool *result_r)
{
/* FIXME: eventually we'd want to support only yes/no */
if (strcasecmp(value, "yes") == 0 ||
strcasecmp(value, "y") == 0 || strcmp(value, "1") == 0)
*result_r = TRUE;
else if (strcasecmp(value, "no") == 0)
*result_r = FALSE;
else {
ctx->error = p_strdup_printf(ctx->parser_pool,
"Invalid boolean value: %s (use yes or no)", value);
return -1;
}
return 0;
}
static int
get_uint(struct setting_parser_context *ctx, const char *value,
unsigned int *result_r)
{
if (str_to_uint(value, result_r) < 0) {
ctx->error = p_strdup_printf(ctx->parser_pool,
"Invalid number %s: %s", value,
str_num_error(value));
return -1;
}
return 0;
}
static int
get_octal(struct setting_parser_context *ctx, const char *value,
unsigned int *result_r)
{
unsigned long long octal;
if (*value != '0')
return get_uint(ctx, value, result_r);
if (str_to_ullong_oct(value, &octal) < 0) {
ctx->error = p_strconcat(ctx->parser_pool, "Invalid number: ",
value, NULL);
return -1;
}
*result_r = (unsigned int)octal;
return 0;
}
int settings_get_time(const char *str, unsigned int *secs_r,
const char **error_r)
{
uintmax_t num, multiply = 1;
const char *p;
if (str_parse_uintmax(str, &num, &p) < 0) {
*error_r = t_strconcat("Invalid time interval: ", str, NULL);
return -1;
}
while (*p == ' ') p++;
if (*p == '\0' && num != 0) {
*error_r = t_strdup_printf("Time interval '%s' is missing units "
"(add e.g. 's' for seconds): ", str);
return -1;
}
switch (i_toupper(*p)) {
case 'S':
multiply = 1;
if (strncasecmp(p, "secs", strlen(p)) == 0 ||
strncasecmp(p, "seconds", strlen(p)) == 0)
p = "";
break;
case 'M':
multiply = 60;
if (strncasecmp(p, "mins", strlen(p)) == 0 ||
strncasecmp(p, "minutes", strlen(p)) == 0)
p = "";
break;
case 'H':
multiply = 60*60;
if (strncasecmp(p, "hours", strlen(p)) == 0)
p = "";
break;
case 'D':
multiply = 60*60*24;
if (strncasecmp(p, "days", strlen(p)) == 0)
p = "";
break;
case 'W':
multiply = 60*60*24*7;
if (strncasecmp(p, "weeks", strlen(p)) == 0)
p = "";
break;
}
if (*p != '\0') {
*error_r = t_strconcat("Invalid time interval: ", str, NULL);
return -1;
}
if (num > UINT_MAX / multiply) {
*error_r = t_strconcat("Time interval is too large: ",
str, NULL);
return -1;
}
*secs_r = num * multiply;
return 0;
}
int settings_get_size(const char *str, uoff_t *bytes_r,
const char **error_r)
{
uintmax_t num, multiply = 1;
const char *p;
if (str_parse_uintmax(str, &num, &p) < 0) {
*error_r = t_strconcat("Invalid size: ", str, NULL);
return -1;
}
while (*p == ' ') p++;
switch (i_toupper(*p)) {
case 'B':
multiply = 1;
p += 1;
break;
case 'K':
multiply = 1024;
p += 1;
break;
case 'M':
multiply = 1024*1024;
p += 1;
break;
case 'G':
multiply = 1024*1024*1024;
p += 1;
break;
case 'T':
multiply = 1024ULL*1024*1024*1024;
p += 1;
break;
}
if (multiply > 1) {
/* Allow: k, ki, kiB */
if (i_toupper(*p) == 'I')
p++;
if (i_toupper(*p) == 'B')
p++;
}
if (*p != '\0') {
*error_r = t_strconcat("Invalid size: ", str, NULL);
return -1;
}
if (num > ((uoff_t)-1) / multiply) {
*error_r = t_strconcat("Size is too large: ", str, NULL);
return -1;
}
*bytes_r = num * multiply;
return 0;
}
static int get_enum(struct setting_parser_context *ctx, const char *value,
char **result_r, const char *allowed_values)
{
const char *p;
while (allowed_values != NULL) {
p = strchr(allowed_values, ':');
if (p == NULL) {
if (strcmp(allowed_values, value) == 0)
break;
ctx->error = p_strconcat(ctx->parser_pool,
"Invalid value: ",
value, NULL);
return -1;
}
if (strncmp(allowed_values, value, p - allowed_values) == 0 &&
value[p - allowed_values] == '\0')
break;
allowed_values = p + 1;
}
*result_r = p_strdup(ctx->set_pool, value);
return 0;
}
static void
setting_link_init_set_struct(struct setting_parser_context *ctx,
struct setting_link *link)
{
void *ptr;
link->set_struct = p_malloc(ctx->set_pool, link->info->struct_size);
if ((ctx->flags & SETTINGS_PARSER_FLAG_TRACK_CHANGES) != 0) {
link->change_struct =
p_malloc(ctx->set_pool, link->info->struct_size);
array_append(link->change_array, &link->change_struct, 1);
}
setting_parser_copy_defaults(ctx, link->info, link);
array_append(link->array, &link->set_struct, 1);
if (link->info->parent_offset != (size_t)-1 && link->parent != NULL) {
ptr = STRUCT_MEMBER_P(link->set_struct,
link->info->parent_offset);
*((void **)ptr) = link->parent->set_struct;
}
}
static int ATTR_NULL(2)
setting_link_add(struct setting_parser_context *ctx,
const struct setting_define *def,
const struct setting_link *link_copy, char *key)
{
struct setting_link *link;
link = hash_table_lookup(ctx->links, key);
if (link != NULL) {
if (link->parent == link_copy->parent &&
link->info == link_copy->info &&
(def == NULL || def->type == SET_DEFLIST_UNIQUE))
return 0;
ctx->error = p_strconcat(ctx->parser_pool, key,
" already exists", NULL);
return -1;
}
link = p_new(ctx->parser_pool, struct setting_link, 1);
*link = *link_copy;
link->full_key = key;
i_assert(hash_table_lookup(ctx->links, key) == NULL);
hash_table_insert(ctx->links, key, link);
if (link->info->struct_size != 0)
setting_link_init_set_struct(ctx, link);
return 0;
}
static int ATTR_NULL(3, 8)
get_deflist(struct setting_parser_context *ctx, struct setting_link *parent,
const struct setting_define *def,
const struct setting_parser_info *info,
const char *key, const char *value, ARRAY_TYPE(void_array) *result,
ARRAY_TYPE(void_array) *change_result)
{
struct setting_link new_link;
const char *const *list;
char *full_key;
i_assert(info->defines != NULL || info == &strlist_info);
if (!array_is_created(result))
p_array_init(result, ctx->set_pool, 5);
if (change_result != NULL && !array_is_created(change_result))
p_array_init(change_result, ctx->set_pool, 5);
memset(&new_link, 0, sizeof(new_link));
new_link.parent = parent;
new_link.info = info;
new_link.array = result;
new_link.change_array = change_result;
if (info == &strlist_info) {
/* there are no sections below strlist, so allow referencing it
without the key (e.g. plugin/foo instead of plugin/0/foo) */
full_key = p_strdup(ctx->parser_pool, key);
if (setting_link_add(ctx, def, &new_link, full_key) < 0)
return -1;
}
list = t_strsplit(value, ",\t ");
for (; *list != NULL; list++) {
if (**list == '\0')
continue;
full_key = p_strconcat(ctx->parser_pool, key,
SETTINGS_SEPARATOR_S, *list, NULL);
if (setting_link_add(ctx, def, &new_link, full_key) < 0)
return -1;
}
return 0;
}
static int
get_in_port_zero(struct setting_parser_context *ctx, const char *value,
in_port_t *result_r)
{
if (net_str2port_zero(value, result_r) < 0) {
ctx->error = p_strdup_printf(ctx->parser_pool,
"Invalid port number %s", value);
return -1;
}
return 0;
}
static int
settings_parse(struct setting_parser_context *ctx, struct setting_link *link,
const struct setting_define *def,
const char *key, const char *value)
{
void *ptr, *change_ptr;
const void *ptr2;
const char *error;
while (def->type == SET_ALIAS) {
i_assert(def != link->info->defines);
def--;
}
ctx->prev_info = link->info;
if (link->set_struct == NULL)
setting_link_init_set_struct(ctx, link);
change_ptr = link->change_struct == NULL ? NULL :
STRUCT_MEMBER_P(link->change_struct, def->offset);
ptr = STRUCT_MEMBER_P(link->set_struct, def->offset);
switch (def->type) {
case SET_BOOL:
if (get_bool(ctx, value, (bool *)ptr) < 0)
return -1;
break;
case SET_UINT:
if (get_uint(ctx, value, (unsigned int *)ptr) < 0)
return -1;
break;
case SET_UINT_OCT:
if (get_octal(ctx, value, (unsigned int *)ptr) < 0)
return -1;
break;
case SET_TIME:
if (settings_get_time(value, (unsigned int *)ptr, &error) < 0) {
ctx->error = p_strdup(ctx->parser_pool, error);
return -1;
}
break;
case SET_SIZE:
if (settings_get_size(value, (uoff_t *)ptr, &error) < 0) {
ctx->error = p_strdup(ctx->parser_pool, error);
return -1;
}
break;
case SET_IN_PORT:
if (get_in_port_zero(ctx, value, (in_port_t *)ptr) < 0)
return -1;
break;
case SET_STR:
*((char **)ptr) = p_strdup(ctx->set_pool, value);
break;
case SET_STR_VARS:
*((char **)ptr) = p_strconcat(ctx->set_pool,
ctx->str_vars_are_expanded ?
SETTING_STRVAR_EXPANDED :
SETTING_STRVAR_UNEXPANDED,
value, NULL);
break;
case SET_ENUM:
/* get the available values from default string */
i_assert(link->info->defaults != NULL);
ptr2 = CONST_STRUCT_MEMBER_P(link->info->defaults, def->offset);
if (get_enum(ctx, value, (char **)ptr,
*(const char *const *)ptr2) < 0)
return -1;
break;
case SET_DEFLIST:
case SET_DEFLIST_UNIQUE:
ctx->prev_info = def->list_info;
return get_deflist(ctx, link, def, def->list_info,
key, value, (ARRAY_TYPE(void_array) *)ptr,
(ARRAY_TYPE(void_array) *)change_ptr);
case SET_STRLIST: {
ctx->prev_info = &strlist_info;
if (get_deflist(ctx, link, NULL, &strlist_info, key, value,
(ARRAY_TYPE(void_array) *)ptr, NULL) < 0)
return -1;
break;
}
case SET_ALIAS:
i_unreached();
break;
}
if (change_ptr != NULL)
*((char *)change_ptr) = 1;
return 0;
}
static bool
settings_find_key_nth(struct setting_parser_context *ctx, const char *key,
unsigned int *n, const struct setting_define **def_r,
struct setting_link **link_r)
{
const struct setting_define *def;
struct setting_link *link;
const char *end, *parent_key;
unsigned int i;
/* try to find from roots */
for (i = *n; i < ctx->root_count; i++) {
def = setting_define_find(ctx->roots[i].info, key);
if (def != NULL) {
*n = i + 1;
*def_r = def;
*link_r = &ctx->roots[i];
return TRUE;
}
}
if (*n > ctx->root_count)
return FALSE;
*n += 1;
/* try to find from links */
end = strrchr(key, SETTINGS_SEPARATOR);
if (end == NULL)
return FALSE;
parent_key = t_strdup_until(key, end);
link = hash_table_lookup(ctx->links, parent_key);
if (link == NULL) {
/* maybe this is the first strlist value */
unsigned int parent_n = 0;
const struct setting_define *parent_def;
struct setting_link *parent_link;
if (!settings_find_key_nth(ctx, parent_key, &parent_n,
&parent_def, &parent_link))
return FALSE;
if (parent_def == NULL) {
/* we'll get here with e.g. "plugin/a/b=val".
not sure if we should ever do anything here.. */
if (parent_link->full_key == NULL ||
strcmp(parent_link->full_key, parent_key) != 0)
return FALSE;
} else {
if (parent_def->type != SET_STRLIST)
return FALSE;
}
/* setting parent_key=0 adds it to links list */
if (settings_parse_keyvalue(ctx, parent_key, "0") <= 0)
return FALSE;
link = hash_table_lookup(ctx->links, parent_key);
i_assert(link != NULL);
}
*link_r = link;
if (link->info == &strlist_info) {
*def_r = NULL;
return TRUE;
} else {
*def_r = setting_define_find(link->info, end + 1);
return *def_r != NULL;
}
}
static bool
settings_find_key(struct setting_parser_context *ctx, const char *key,
const struct setting_define **def_r,
struct setting_link **link_r)
{
unsigned int n = 0;
return settings_find_key_nth(ctx, key, &n, def_r, link_r);
}
static void
settings_parse_strlist(struct setting_parser_context *ctx,
struct setting_link *link,
const char *key, const char *value)
{
void *const *items;
void *vkey, *vvalue;
unsigned int i, count;
key = strrchr(key, SETTINGS_SEPARATOR) + 1;
vvalue = p_strdup(ctx->set_pool, value);
/* replace if it already exists */
items = array_get(link->array, &count);
for (i = 0; i < count; i += 2) {
if (strcmp(items[i], key) == 0) {
array_idx_set(link->array, i + 1, &vvalue);
return;
}
}
vkey = p_strdup(ctx->set_pool, key);
array_append(link->array, &vkey, 1);
array_append(link->array, &vvalue, 1);
}
static int settings_parse_keyvalue(struct setting_parser_context *ctx,
const char *key, const char *value)
{
const struct setting_define *def;
struct setting_link *link;
unsigned int n = 0;
if (!settings_find_key_nth(ctx, key, &n, &def, &link)) {
ctx->error = p_strconcat(ctx->parser_pool,
"Unknown setting: ", key, NULL);
return 0;
}
do {
if (def == NULL) {
i_assert(link->info == &strlist_info);
settings_parse_strlist(ctx, link, key, value);
return 1;
}
if (settings_parse(ctx, link, def, key, value) < 0)
return -1;
/* there may be more instances of the setting */
} while (settings_find_key_nth(ctx, key, &n, &def, &link));
return 1;
}
bool settings_parse_is_valid_key(struct setting_parser_context *ctx,
const char *key)
{
const struct setting_define *def;
struct setting_link *link;
return settings_find_key(ctx, key, &def, &link);
}
const char *settings_parse_unalias(struct setting_parser_context *ctx,
const char *key)
{
const struct setting_define *def;
struct setting_link *link;
if (!settings_find_key(ctx, key, &def, &link))
return NULL;
if (def == NULL) {
/* strlist */
i_assert(link->info == &strlist_info);
return key;
}
while (def->type == SET_ALIAS) {
i_assert(def != link->info->defines);
def--;
}
return def->key;
}
const void *
settings_parse_get_value(struct setting_parser_context *ctx,
const char *key, enum setting_type *type_r)
{
const struct setting_define *def;
struct setting_link *link;
if (!settings_find_key(ctx, key, &def, &link))
return NULL;
if (link->set_struct == NULL || def == NULL)
return NULL;
*type_r = def->type;
return STRUCT_MEMBER_P(link->set_struct, def->offset);
}
bool settings_parse_is_changed(struct setting_parser_context *ctx,
const char *key)
{
const struct setting_define *def;
struct setting_link *link;
const unsigned char *p;
if (!settings_find_key(ctx, key, &def, &link))
return FALSE;
if (link->change_struct == NULL || def == NULL)
return FALSE;
p = STRUCT_MEMBER_P(link->change_struct, def->offset);
return *p;
}
int settings_parse_line(struct setting_parser_context *ctx, const char *line)
{
const char *key, *value;
int ret;
ctx->error = NULL;
ctx->prev_info = NULL;
key = line;
value = strchr(line, '=');
if (value == NULL) {
ctx->error = "Missing '='";
return -1;
}
if (key == value) {
ctx->error = "Missing key name ('=' at the beginning of line)";
return -1;
}
T_BEGIN {
key = t_strdup_until(key, value);
ret = settings_parse_keyvalue(ctx, key, value + 1);
} T_END;
return ret;
}
const struct setting_parser_info *
settings_parse_get_prev_info(struct setting_parser_context *ctx)
{
return ctx->prev_info;
}
static const char *settings_translate_lf(const char *value)
{
char *dest, *p;
if (strchr(value, SETTING_STREAM_LF_CHAR[0]) == NULL)
return value;
dest = t_strdup_noconst(value);
for (p = dest; *p != '\0'; p++) {
if (*p == SETTING_STREAM_LF_CHAR[0])
*p = '\n';
}
return dest;
}
int settings_parse_stream(struct setting_parser_context *ctx,
struct istream *input)
{
bool ignore_unknown_keys =
(ctx->flags & SETTINGS_PARSER_FLAG_IGNORE_UNKNOWN_KEYS) != 0;
const char *line;
int ret;
while ((line = i_stream_next_line(input)) != NULL) {
if (*line == '\0') {
/* empty line finishes it */
return 0;
}
ctx->linenum++;
if (ctx->linenum == 1 && strncmp(line, "ERROR ", 6) == 0) {
ctx->error = p_strdup(ctx->parser_pool, line + 6);
return -1;
}
T_BEGIN {
line = settings_translate_lf(line);
ret = settings_parse_line(ctx, line);
} T_END;
if (ret < 0 || (ret == 0 && !ignore_unknown_keys)) {
ctx->error = p_strdup_printf(ctx->parser_pool,
"Line %u: %s", ctx->linenum, ctx->error);
return -1;
}
}
return 1;
}
int settings_parse_stream_read(struct setting_parser_context *ctx,
struct istream *input)
{
int ret;
do {
if ((ret = settings_parse_stream(ctx, input)) < 0)
return -1;
if (ret == 0) {
/* empty line read */
return 0;
}
} while ((ret = i_stream_read(input)) > 0);
switch (ret) {
case -1:
if (ctx->error != NULL)
break;
if (input->stream_errno != 0) {
ctx->error = p_strdup_printf(ctx->parser_pool,
"read(%s) failed: %s", i_stream_get_name(input),
i_stream_get_error(input));
} else if (input->v_offset == 0) {
ctx->error = p_strdup_printf(ctx->parser_pool,
"read(%s) disconnected before receiving any data",
i_stream_get_name(input));
} else {
ctx->error = p_strdup_printf(ctx->parser_pool,
"read(%s) disconnected before receiving "
"end-of-settings line",
i_stream_get_name(input));