-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
options.c
3123 lines (2934 loc) · 87.7 KB
/
options.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
/*
* options.c -- options functions.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <errno.h>
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#endif
#ifdef HAVE_SSL
#include <openssl/ssl.h>
#include <openssl/x509v3.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "options.h"
#include "query.h"
#include "tsig.h"
#include "ixfr.h"
#include "difffile.h"
#include "rrl.h"
#include "bitset.h"
#include "xfrd.h"
#include "configparser.h"
config_parser_state_type* cfg_parser = 0;
extern FILE* c_in, *c_out;
int c_parse(void);
int c_lex(void);
int c_wrap(void);
int c_lex_destroy(void);
extern char* c_text;
static int
rbtree_strcmp(const void* p1, const void* p2)
{
if(p1 == NULL && p2 == NULL) return 0;
if(p1 == NULL) return -1;
if(p2 == NULL) return 1;
return strcmp((const char*)p1, (const char*)p2);
}
struct nsd_options*
nsd_options_create(region_type* region)
{
struct nsd_options* opt;
opt = (struct nsd_options*)region_alloc(region, sizeof(
struct nsd_options));
opt->region = region;
opt->zone_options = rbtree_create(region,
(int (*)(const void *, const void *)) dname_compare);
opt->configfile = NULL;
opt->zonestatnames = rbtree_create(opt->region, rbtree_strcmp);
opt->patterns = rbtree_create(region, rbtree_strcmp);
opt->keys = rbtree_create(region, rbtree_strcmp);
opt->tls_auths = rbtree_create(region, rbtree_strcmp);
opt->ip_addresses = NULL;
opt->ip_transparent = 0;
opt->ip_freebind = 0;
opt->send_buffer_size = 0;
opt->receive_buffer_size = 0;
opt->debug_mode = 0;
opt->verbosity = 0;
opt->hide_version = 0;
opt->hide_identity = 0;
opt->drop_updates = 0;
opt->do_ip4 = 1;
opt->do_ip6 = 1;
opt->identity = 0;
opt->version = 0;
opt->nsid = 0;
opt->logfile = 0;
opt->log_only_syslog = 0;
opt->log_time_ascii = 1;
opt->log_time_iso = 0;
opt->round_robin = 0; /* also packet.h::round_robin */
opt->minimal_responses = 0; /* also packet.h::minimal_responses */
opt->confine_to_zone = 0;
opt->refuse_any = 0;
opt->server_count = 1;
opt->cpu_affinity = NULL;
opt->service_cpu_affinity = NULL;
opt->tcp_count = 100;
opt->tcp_reject_overflow = 0;
opt->tcp_query_count = 0;
opt->tcp_timeout = TCP_TIMEOUT;
opt->tcp_mss = 0;
opt->outgoing_tcp_mss = 0;
opt->ipv4_edns_size = EDNS_MAX_MESSAGE_LEN;
opt->ipv6_edns_size = EDNS_MAX_MESSAGE_LEN;
opt->pidfile = PIDFILE;
opt->port = UDP_PORT;
/* deprecated? opt->port = TCP_PORT; */
opt->reuseport = 0;
opt->xfrd_tcp_max = 128;
opt->xfrd_tcp_pipeline = 128;
opt->statistics = 0;
opt->chroot = 0;
opt->username = USER;
opt->zonesdir = ZONESDIR;
opt->xfrdfile = XFRDFILE;
opt->xfrdir = XFRDIR;
opt->zonelistfile = ZONELISTFILE;
#ifdef RATELIMIT
opt->rrl_size = RRL_BUCKETS;
opt->rrl_slip = RRL_SLIP;
opt->rrl_ipv4_prefix_length = RRL_IPV4_PREFIX_LENGTH;
opt->rrl_ipv6_prefix_length = RRL_IPV6_PREFIX_LENGTH;
# ifdef RATELIMIT_DEFAULT_OFF
opt->rrl_ratelimit = 0;
opt->rrl_whitelist_ratelimit = 0;
# else
opt->rrl_ratelimit = RRL_LIMIT/2;
opt->rrl_whitelist_ratelimit = RRL_WLIST_LIMIT/2;
# endif
#endif
#ifdef USE_DNSTAP
opt->dnstap_enable = 0;
opt->dnstap_socket_path = DNSTAP_SOCKET_PATH;
opt->dnstap_ip = "";
opt->dnstap_tls = 1;
opt->dnstap_tls_server_name = NULL;
opt->dnstap_tls_cert_bundle = NULL;
opt->dnstap_tls_client_key_file = NULL;
opt->dnstap_tls_client_cert_file = NULL;
opt->dnstap_send_identity = 0;
opt->dnstap_send_version = 0;
opt->dnstap_identity = NULL;
opt->dnstap_version = NULL;
opt->dnstap_log_auth_query_messages = 0;
opt->dnstap_log_auth_response_messages = 0;
#endif
opt->reload_config = 0;
opt->zonefiles_check = 1;
opt->zonefiles_write = ZONEFILES_WRITE_INTERVAL;
opt->xfrd_reload_timeout = 1;
opt->tls_service_key = NULL;
opt->tls_service_ocsp = NULL;
opt->tls_service_pem = NULL;
opt->tls_port = TLS_PORT;
opt->tls_auth_port = NULL;
opt->tls_cert_bundle = NULL;
opt->tls_auth_xfr_only = 0;
opt->proxy_protocol_port = NULL;
opt->answer_cookie = 0;
opt->cookie_secret = NULL;
opt->cookie_staging_secret = NULL;
opt->cookie_secret_file = NULL;
opt->cookie_secret_file_is_default = 1;
opt->control_enable = 0;
opt->control_interface = NULL;
opt->control_port = NSD_CONTROL_PORT;
opt->server_key_file = CONFIGDIR"/nsd_server.key";
opt->server_cert_file = CONFIGDIR"/nsd_server.pem";
opt->control_key_file = CONFIGDIR"/nsd_control.key";
opt->control_cert_file = CONFIGDIR"/nsd_control.pem";
opt->verify_enable = 0;
opt->verify_ip_addresses = NULL;
opt->verify_port = VERIFY_PORT;
opt->verify_zones = 1;
opt->verifier = NULL;
opt->verifier_count = 1;
opt->verifier_feed_zone = 1;
opt->verifier_timeout = 0;
return opt;
}
int
nsd_options_insert_zone(struct nsd_options* opt, struct zone_options* zone)
{
/* create dname for lookup */
const dname_type* dname = dname_parse(opt->region, zone->name);
if(!dname)
return 0;
zone->node.key = dname;
if(!rbtree_insert(opt->zone_options, (rbnode_type*)zone))
return 0;
return 1;
}
int
nsd_options_insert_pattern(struct nsd_options* opt,
struct pattern_options* pat)
{
if(!pat->pname)
return 0;
pat->node.key = pat->pname;
if(!rbtree_insert(opt->patterns, (rbnode_type*)pat))
return 0;
return 1;
}
void
warn_if_directory(const char* filetype, FILE* f, const char* fname)
{
if(fileno(f) != -1) {
struct stat st;
memset(&st, 0, sizeof(st));
if(fstat(fileno(f), &st) != -1) {
if(S_ISDIR(st.st_mode)) {
log_msg(LOG_WARNING, "trying to read %s but it is a directory: %s", filetype, fname);
}
}
}
}
int
parse_options_file(struct nsd_options* opt, const char* file,
void (*err)(void*,const char*), void* err_arg,
struct nsd_options* old_opts)
{
FILE *in = 0;
struct pattern_options* pat;
struct acl_options* acl;
if(!cfg_parser) {
cfg_parser = (config_parser_state_type*)region_alloc(
opt->region, sizeof(config_parser_state_type));
cfg_parser->chroot = 0;
}
cfg_parser->err = err;
cfg_parser->err_arg = err_arg;
cfg_parser->filename = (char*)file;
cfg_parser->line = 1;
cfg_parser->errors = 0;
cfg_parser->opt = opt;
cfg_parser->pattern = NULL;
cfg_parser->zone = NULL;
cfg_parser->key = NULL;
cfg_parser->tls_auth = NULL;
in = fopen(cfg_parser->filename, "r");
if(!in) {
if(err) {
char m[MAXSYSLOGMSGLEN];
snprintf(m, sizeof(m), "Could not open %s: %s\n",
file, strerror(errno));
err(err_arg, m);
} else {
fprintf(stderr, "Could not open %s: %s\n",
file, strerror(errno));
}
return 0;
}
warn_if_directory("configfile", in, file);
c_in = in;
c_parse();
fclose(in);
opt->configfile = region_strdup(opt->region, file);
/* Set default cookie_secret_file value */
if(opt->cookie_secret_file_is_default && !opt->cookie_secret_file) {
opt->cookie_secret_file =
region_strdup(opt->region, COOKIESECRETSFILE);
}
/* Semantic errors */
if(opt->cookie_staging_secret && !opt->cookie_secret) {
c_error("a cookie-staging-secret cannot be configured without "
"also providing a cookie-secret");
}
RBTREE_FOR(pat, struct pattern_options*, opt->patterns)
{
struct pattern_options* old_pat =
old_opts ? pattern_options_find(old_opts, pat->pname)
: NULL;
/* lookup keys for acls */
for(acl=pat->allow_notify; acl; acl=acl->next)
{
if(acl->nokey || acl->blocked)
continue;
acl->key_options = key_options_find(opt, acl->key_name);
if(!acl->key_options)
c_error("key %s in pattern %s could not be found",
acl->key_name, pat->pname);
}
for(acl=pat->notify; acl; acl=acl->next)
{
if(acl->nokey || acl->blocked)
continue;
acl->key_options = key_options_find(opt, acl->key_name);
if(!acl->key_options)
c_error("key %s in pattern %s could not be found",
acl->key_name, pat->pname);
}
for(acl=pat->request_xfr; acl; acl=acl->next)
{
/* Find tls_auth */
if (!acl->tls_auth_name)
; /* pass */
else if (!(acl->tls_auth_options =
tls_auth_options_find(opt, acl->tls_auth_name)))
c_error("tls_auth %s in pattern %s could not be found",
acl->tls_auth_name, pat->pname);
/* Find key */
if(acl->nokey || acl->blocked)
continue;
acl->key_options = key_options_find(opt, acl->key_name);
if(!acl->key_options)
c_error("key %s in pattern %s could not be found",
acl->key_name, pat->pname);
}
for(acl=pat->provide_xfr; acl; acl=acl->next)
{
/* Find tls_auth */
if (acl->tls_auth_name) {
if (!(acl->tls_auth_options =
tls_auth_options_find(opt, acl->tls_auth_name)))
c_error("tls_auth %s in pattern %s could not be found",
acl->tls_auth_name, pat->pname);
}
if(acl->nokey || acl->blocked)
continue;
acl->key_options = key_options_find(opt, acl->key_name);
if(!acl->key_options)
c_error("key %s in pattern %s could not be found",
acl->key_name, pat->pname);
}
for(acl=pat->allow_query; acl; acl=acl->next)
{
if(acl->nokey || acl->blocked)
continue;
acl->key_options = key_options_find(opt, acl->key_name);
if(!acl->key_options)
c_error("key %s in pattern %s could not be found",
acl->key_name, pat->pname);
}
/* lookup zones for catalog-producer-zone options */
if(pat->catalog_producer_zone) {
struct zone_options* zopt;
const dname_type *dname = dname_parse(opt->region,
pat->catalog_producer_zone);
if(dname == NULL) {
; /* pass; already erred during parsing */
} else if (!(zopt = zone_options_find(opt, dname))) {
c_error("catalog producer zone %s in pattern "
"%s could not be found",
pat->catalog_producer_zone,
pat->pname);
} else if (!zone_is_catalog_producer(zopt)) {
c_error("catalog-producer-zone %s in pattern "
"%s is not configered as a "
"catalog: producer",
pat->catalog_producer_zone,
pat->pname);
}
}
if( !old_opts /* Okay to add a cat producer member zone pat */
|| (!old_pat) /* But not to add, change or del an existing */
|| ( old_pat && !old_pat->catalog_producer_zone
&& !pat->catalog_producer_zone)
|| ( old_pat && old_pat->catalog_producer_zone
&& pat->catalog_producer_zone
&& strcmp( old_pat->catalog_producer_zone
, pat->catalog_producer_zone) == 0)){
; /* No existing catalog producer member zone added
* or changed. Everyting is fine: pass */
} else {
c_error("catalog-producer-zone in pattern %s cannot "
"be removed or changed on a running NSD",
pat->pname);
}
}
if(cfg_parser->errors > 0)
{
if(err) {
char m[MAXSYSLOGMSGLEN];
snprintf(m, sizeof(m), "read %s failed: %d errors in "
"configuration file\n", file,
cfg_parser->errors);
err(err_arg, m);
} else {
fprintf(stderr, "read %s failed: %d errors in "
"configuration file\n", file,
cfg_parser->errors);
}
return 0;
}
return 1;
}
void options_zonestatnames_create(struct nsd_options* opt)
{
struct zone_options* zopt;
/* allocate "" as zonestat 0, for zones without a zonestat */
if(!rbtree_search(opt->zonestatnames, "")) {
struct zonestatname* n;
n = (struct zonestatname*)region_alloc_zero(opt->region,
sizeof(*n));
n->node.key = region_strdup(opt->region, "");
if(!n->node.key) {
log_msg(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(1);
}
n->id = (unsigned)(opt->zonestatnames->count);
rbtree_insert(opt->zonestatnames, (rbnode_type*)n);
}
RBTREE_FOR(zopt, struct zone_options*, opt->zone_options) {
/* insert into tree, so that when read in later id exists */
(void)getzonestatid(opt, zopt);
}
}
#define ZONELIST_HEADER "# NSD zone list\n# name pattern\n"
static int
comp_zonebucket(const void* a, const void* b)
{
/* the line size is much smaller than max-int, and positive,
* so the subtraction works */
return *(const int*)b - *(const int*)a;
}
/* insert free entry into zonelist free buckets */
static void
zone_list_free_insert(struct nsd_options* opt, int linesize, off_t off)
{
struct zonelist_free* e;
struct zonelist_bucket* b = (struct zonelist_bucket*)rbtree_search(
opt->zonefree, &linesize);
if(!b) {
b = region_alloc_zero(opt->region, sizeof(*b));
b->linesize = linesize;
b->node = *RBTREE_NULL;
b->node.key = &b->linesize;
rbtree_insert(opt->zonefree, &b->node);
}
e = (struct zonelist_free*)region_alloc_zero(opt->region, sizeof(*e));
e->next = b->list;
b->list = e;
e->off = off;
opt->zonefree_number++;
}
static struct zone_options*
zone_list_member_zone_insert(struct nsd_options* opt, const char* nm,
const char* patnm, int linesize, off_t off, const char* mem_idnm,
new_member_id_type new_member_id)
{
struct pattern_options* pat = pattern_options_find(opt, patnm);
struct catalog_member_zone* cmz = NULL;
struct zone_options* zone;
char member_id_str[MAXDOMAINLEN * 5 + 3] = "ERROR!";
DEBUG(DEBUG_XFRD, 2, (LOG_INFO, "zone_list_zone_insert(\"%s\", \"%s\""
", %d, \"%s\")", nm, patnm, linesize,
(mem_idnm ? mem_idnm : "<NULL>")));
if(!pat) {
log_msg(LOG_ERR, "pattern does not exist for zone %s "
"pattern %s", nm, patnm);
return NULL;
}
zone = pat->catalog_producer_zone
? &(cmz = catalog_member_zone_create(opt->region))->options
: zone_options_create(opt->region);
zone->part_of_config = 0;
zone->name = region_strdup(opt->region, nm);
zone->linesize = linesize;
zone->off = off;
zone->pattern = pat;
if(!nsd_options_insert_zone(opt, zone)) {
log_msg(LOG_ERR, "bad domain name or duplicate zone '%s' "
"pattern %s", nm, patnm);
region_recycle(opt->region, (void*)zone->name, strlen(nm)+1);
region_recycle(opt->region, zone, sizeof(*zone));
return NULL;
}
if(!mem_idnm) {
if(cmz && new_member_id)
new_member_id(cmz);
if(cmz && cmz->member_id) {
/* Assume all bytes of member_id are printable.
* plus 1 for space
*/
zone->linesize += label_length(dname_name(cmz->member_id)) + 1;
DEBUG(DEBUG_XFRD, 2, (LOG_INFO, "new linesize: %d",
(int)zone->linesize));
}
} else if(!cmz)
log_msg(LOG_ERR, "member ID '%s' given, but no catalog-producer-"
"zone value provided in zone '%s' or pattern '%s'",
mem_idnm, nm, patnm);
else if(snprintf(member_id_str, sizeof(member_id_str),
"%s.zones.%s", mem_idnm, pat->catalog_producer_zone) >=
(int)sizeof(member_id_str))
log_msg(LOG_ERR, "syntax error in member ID '%s.zones.%s' for "
"zone '%s'", mem_idnm, pat->catalog_producer_zone, nm);
else if(!(cmz->member_id = dname_parse(opt->region, member_id_str)))
log_msg(LOG_ERR, "parse error in member ID '%s' for "
"zone '%s'", member_id_str, nm);
return zone;
}
struct zone_options*
zone_list_zone_insert(struct nsd_options* opt,const char* nm,const char* patnm)
{
return zone_list_member_zone_insert(opt, nm, patnm, 0, 0, NULL, NULL);
}
int
parse_zone_list_file(struct nsd_options* opt)
{
/* zonelist looks like this:
# name pattern
add example.com master
del example.net slave
add foo.bar.nl slave
add rutabaga.uk config
*/
char hdr[64];
char buf[1024];
/* create empty data structures */
opt->zonefree = rbtree_create(opt->region, comp_zonebucket);
opt->zonelist = NULL;
opt->zonefree_number = 0;
opt->zonelist_off = 0;
/* try to open the zonelist file, an empty or nonexist file is OK */
opt->zonelist = fopen(opt->zonelistfile, "r+");
if(!opt->zonelist) {
if(errno == ENOENT)
return 1; /* file does not exist, it is created later */
log_msg(LOG_ERR, "could not open zone list %s: %s", opt->zonelistfile,
strerror(errno));
return 0;
}
/* read header */
hdr[strlen(ZONELIST_HEADER)] = 0;
if(fread(hdr, 1, strlen(ZONELIST_HEADER), opt->zonelist) !=
strlen(ZONELIST_HEADER) || strncmp(hdr, ZONELIST_HEADER,
strlen(ZONELIST_HEADER)) != 0) {
log_msg(LOG_ERR, "zone list %s contains bad header\n", opt->zonelistfile);
fclose(opt->zonelist);
opt->zonelist = NULL;
return 0;
}
buf[sizeof(buf)-1]=0;
/* read entries in file */
while(fgets(buf, sizeof(buf), opt->zonelist)) {
/* skip comments and empty lines */
if(buf[0] == 0 || buf[0] == '\n' || buf[0] == '#')
continue;
if(strncmp(buf, "add ", 4) == 0) {
int linesize = strlen(buf);
/* parse the 'add' line */
/* pick last space on the line, so that the domain
* name can have a space in it (but not the pattern)*/
char* space = strrchr(buf+4, ' ');
char* nm, *patnm;
if(!space) {
/* parse error */
log_msg(LOG_ERR, "parse error in %s: '%s'",
opt->zonelistfile, buf);
continue;
}
nm = buf+4;
*space = 0;
patnm = space+1;
if(linesize && buf[linesize-1] == '\n')
buf[linesize-1] = 0;
/* store offset and line size for zone entry */
/* and create zone entry in zonetree */
(void)zone_list_member_zone_insert(opt, nm, patnm,
linesize, ftello(opt->zonelist)-linesize,
NULL, NULL);
} else if(strncmp(buf, "cat ", 4) == 0) {
int linesize = strlen(buf);
/* parse the 'add' line */
/* pick last space on the line, so that the domain
* name can have a space in it (but not the pattern)*/
char* nm = buf + 4;
char* mem_idnm = strrchr(nm, ' '), *patnm;
if(!mem_idnm) {
/* parse error */
log_msg(LOG_ERR, "parse error in %s: '%s'",
opt->zonelistfile, buf);
continue;
}
*mem_idnm++ = 0;
patnm = strrchr(nm, ' ');
if(!patnm) {
*--mem_idnm = ' ';
/* parse error */
log_msg(LOG_ERR, "parse error in %s: '%s'",
opt->zonelistfile, buf);
continue;
}
*patnm++ = 0;
if(linesize && buf[linesize-1] == '\n')
buf[linesize-1] = 0;
/* store offset and line size for zone entry */
/* and create zone entry in zonetree */
(void)zone_list_member_zone_insert(opt, nm, patnm,
linesize, ftello(opt->zonelist)-linesize,
mem_idnm, NULL);
} else if(strncmp(buf, "del ", 4) == 0) {
/* store offset and line size for deleted entry */
int linesize = strlen(buf);
zone_list_free_insert(opt, linesize,
ftello(opt->zonelist)-linesize);
} else {
log_msg(LOG_WARNING, "bad data in %s, '%s'", opt->zonelistfile,
buf);
}
}
/* store EOF offset */
opt->zonelist_off = ftello(opt->zonelist);
return 1;
}
void
zone_options_delete(struct nsd_options* opt, struct zone_options* zone)
{
struct catalog_member_zone* member_zone = as_catalog_member_zone(zone);
rbtree_delete(opt->zone_options, zone->node.key);
region_recycle(opt->region, (void*)zone->node.key, dname_total_size(
(dname_type*)zone->node.key));
if(!member_zone) {
region_recycle(opt->region, zone, sizeof(*zone));
return;
}
/* Because catalog member zones are in xfrd only deleted through
* catalog_del_consumer_member_zone() or through
* xfrd_del_catalog_producer_member(), which both clear the node,
* and because member zones in the main and serve processes are not
* indexed, *member_zone->node == *RBTREE_NULL.
* member_id is cleared too by those delete function, but there may be
* leftover member_id's from the initial zone.list processing, which
* made it to the main and serve processes.
*/
assert(!memcmp(&member_zone->node, RBTREE_NULL, sizeof(*RBTREE_NULL)));
if(member_zone->member_id) {
region_recycle(opt->region, (void*)member_zone->member_id,
dname_total_size(member_zone->member_id));
}
region_recycle(opt->region, member_zone, sizeof(*member_zone));
}
/* add a new zone to the zonelist */
struct zone_options*
zone_list_add_or_cat(struct nsd_options* opt, const char* zname,
const char* pname, new_member_id_type new_member_id)
{
int r;
struct zonelist_free* e;
struct zonelist_bucket* b;
char zone_list_line[6 + 5 * MAXDOMAINLEN + 2024 + 65];
struct catalog_member_zone* cmz;
/* create zone entry */
struct zone_options* zone = zone_list_member_zone_insert(
opt, zname, pname, 6 + strlen(zname) + strlen(pname),
0, NULL, new_member_id);
if(!zone)
return NULL;
if(zone_is_catalog_producer_member(zone)
&& (cmz = as_catalog_member_zone(zone))
&& cmz->member_id) {
snprintf(zone_list_line, sizeof(zone_list_line),
"cat %s %s %.*s\n", zname, pname,
(int)label_length(dname_name(cmz->member_id)),
(const char*)dname_name(cmz->member_id) + 1);
} else {
snprintf(zone_list_line, sizeof(zone_list_line),
"add %s %s\n", zname, pname);
}
/* use free entry or append to file or create new file */
if(!opt->zonelist || opt->zonelist_off == 0) {
/* create new file */
if(opt->zonelist) fclose(opt->zonelist);
opt->zonelist = fopen(opt->zonelistfile, "w+");
if(!opt->zonelist) {
log_msg(LOG_ERR, "could not create zone list %s: %s",
opt->zonelistfile, strerror(errno));
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
r = fprintf(opt->zonelist, ZONELIST_HEADER);
if(r != strlen(ZONELIST_HEADER)) {
if(r == -1)
log_msg(LOG_ERR, "could not write to %s: %s",
opt->zonelistfile, strerror(errno));
else log_msg(LOG_ERR, "partial write to %s: disk full",
opt->zonelistfile);
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
zone->off = ftello(opt->zonelist);
if(zone->off == -1)
log_msg(LOG_ERR, "ftello(%s): %s", opt->zonelistfile, strerror(errno));
r = fprintf(opt->zonelist, "%s", zone_list_line);
if(r != zone->linesize) {
if(r == -1)
log_msg(LOG_ERR, "could not write to %s: %s",
opt->zonelistfile, strerror(errno));
else log_msg(LOG_ERR, "partial write to %s: disk full",
opt->zonelistfile);
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
opt->zonelist_off = ftello(opt->zonelist);
if(opt->zonelist_off == -1)
log_msg(LOG_ERR, "ftello(%s): %s", opt->zonelistfile, strerror(errno));
if(fflush(opt->zonelist) != 0) {
log_msg(LOG_ERR, "fflush %s: %s", opt->zonelistfile, strerror(errno));
}
return zone;
}
b = (struct zonelist_bucket*)rbtree_search(opt->zonefree,
&zone->linesize);
if(!b || b->list == NULL) {
/* no empty place, append to file */
zone->off = opt->zonelist_off;
if(fseeko(opt->zonelist, zone->off, SEEK_SET) == -1) {
log_msg(LOG_ERR, "fseeko(%s): %s", opt->zonelistfile, strerror(errno));
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
r = fprintf(opt->zonelist, "%s", zone_list_line);
if(r != zone->linesize) {
if(r == -1)
log_msg(LOG_ERR, "could not write to %s: %s",
opt->zonelistfile, strerror(errno));
else log_msg(LOG_ERR, "partial write to %s: disk full",
opt->zonelistfile);
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
opt->zonelist_off += zone->linesize;
if(fflush(opt->zonelist) != 0) {
log_msg(LOG_ERR, "fflush %s: %s", opt->zonelistfile, strerror(errno));
}
return zone;
}
/* reuse empty spot */
e = b->list;
zone->off = e->off;
if(fseeko(opt->zonelist, zone->off, SEEK_SET) == -1) {
log_msg(LOG_ERR, "fseeko(%s): %s", opt->zonelistfile, strerror(errno));
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
r = fprintf(opt->zonelist, "%s", zone_list_line);
if(r != zone->linesize) {
if(r == -1)
log_msg(LOG_ERR, "could not write to %s: %s",
opt->zonelistfile, strerror(errno));
else log_msg(LOG_ERR, "partial write to %s: disk full",
opt->zonelistfile);
log_msg(LOG_ERR, "zone %s could not be added", zname);
zone_options_delete(opt, zone);
return NULL;
}
if(fflush(opt->zonelist) != 0) {
log_msg(LOG_ERR, "fflush %s: %s", opt->zonelistfile, strerror(errno));
}
/* snip off and recycle element */
b->list = e->next;
region_recycle(opt->region, e, sizeof(*e));
if(b->list == NULL) {
rbtree_delete(opt->zonefree, &b->linesize);
region_recycle(opt->region, b, sizeof(*b));
}
opt->zonefree_number--;
return zone;
}
/* remove a zone on the zonelist */
void
zone_list_del(struct nsd_options* opt, struct zone_options* zone)
{
if (zone_is_catalog_consumer_member(zone)) {
/* catalog consumer member zones are not in the zones.list file */
zone_options_delete(opt, zone);
return;
}
/* put its space onto the free entry */
if(fseeko(opt->zonelist, zone->off, SEEK_SET) == -1) {
log_msg(LOG_ERR, "fseeko(%s): %s", opt->zonelistfile, strerror(errno));
return;
}
fprintf(opt->zonelist, "del");
zone_list_free_insert(opt, zone->linesize, zone->off);
/* remove zone_options */
zone_options_delete(opt, zone);
/* see if we need to compact: it is going to halve the zonelist */
if(opt->zonefree_number > opt->zone_options->count) {
zone_list_compact(opt);
} else {
if(fflush(opt->zonelist) != 0) {
log_msg(LOG_ERR, "fflush %s: %s", opt->zonelistfile, strerror(errno));
}
}
}
/* postorder delete of zonelist free space tree */
static void
delbucket(region_type* region, struct zonelist_bucket* b)
{
struct zonelist_free* e, *f;
if(!b || (rbnode_type*)b==RBTREE_NULL)
return;
delbucket(region, (struct zonelist_bucket*)b->node.left);
delbucket(region, (struct zonelist_bucket*)b->node.right);
e = b->list;
while(e) {
f = e->next;
region_recycle(region, e, sizeof(*e));
e = f;
}
region_recycle(region, b, sizeof(*b));
}
/* compact zonelist file */
void
zone_list_compact(struct nsd_options* opt)
{
char outname[1024];
FILE* out;
struct zone_options* zone;
off_t off;
int r;
snprintf(outname, sizeof(outname), "%s~", opt->zonelistfile);
/* useful, when : count-of-free > count-of-used */
/* write zonelist to zonelist~ */
out = fopen(outname, "w+");
if(!out) {
log_msg(LOG_ERR, "could not open %s: %s", outname, strerror(errno));
return;
}
r = fprintf(out, ZONELIST_HEADER);
if(r == -1) {
log_msg(LOG_ERR, "write %s failed: %s", outname,
strerror(errno));
fclose(out);
return;
} else if(r != strlen(ZONELIST_HEADER)) {
log_msg(LOG_ERR, "write %s was partial: disk full",
outname);
fclose(out);
return;
}
off = ftello(out);
if(off == -1) {
log_msg(LOG_ERR, "ftello(%s): %s", outname, strerror(errno));
fclose(out);
return;
}
RBTREE_FOR(zone, struct zone_options*, opt->zone_options) {
struct catalog_member_zone* cmz;
if(zone->part_of_config)
continue;
if(zone_is_catalog_producer_member(zone)
&& (cmz = as_catalog_member_zone(zone))
&& cmz->member_id) {
r = fprintf(out, "cat %s %s %.*s\n", zone->name,
zone->pattern->pname,
(int)label_length(dname_name(cmz->member_id)),
(const char*)dname_name(cmz->member_id) + 1);
} else {
r = fprintf(out, "add %s %s\n", zone->name,
zone->pattern->pname);
}
if(r < 0) {
log_msg(LOG_ERR, "write %s failed: %s", outname,
strerror(errno));
fclose(out);
return;
} else if(r != zone->linesize) {
log_msg(LOG_ERR, "write %s was partial: disk full",
outname);
fclose(out);
return;
}
}
if(fflush(out) != 0) {
log_msg(LOG_ERR, "fflush %s: %s", outname, strerror(errno));
}
/* rename zonelist~ onto zonelist */
if(rename(outname, opt->zonelistfile) == -1) {
log_msg(LOG_ERR, "rename(%s to %s) failed: %s",
outname, opt->zonelistfile, strerror(errno));
fclose(out);
return;
}
fclose(opt->zonelist);
/* set offsets */
RBTREE_FOR(zone, struct zone_options*, opt->zone_options) {
if(zone->part_of_config)
continue;
zone->off = off;
off += zone->linesize;
}
/* empty the free tree */
delbucket(opt->region, (struct zonelist_bucket*)opt->zonefree->root);
opt->zonefree->root = RBTREE_NULL;
opt->zonefree->count = 0;
opt->zonefree_number = 0;
/* finish */
opt->zonelist = out;
opt->zonelist_off = off;
}
/* close zonelist file */
void
zone_list_close(struct nsd_options* opt)
{
if(opt->zonelist) {
fclose(opt->zonelist);
opt->zonelist = NULL;
}
}
static void
c_error_va_list_pos(int showpos, const char* fmt, va_list args)
{
char* at = NULL;
cfg_parser->errors++;
if(showpos && c_text && c_text[0]!=0) {
at = c_text;
}
if(cfg_parser->err) {
char m[MAXSYSLOGMSGLEN];
snprintf(m, sizeof(m), "%s:%d: ", cfg_parser->filename,
cfg_parser->line);
(*cfg_parser->err)(cfg_parser->err_arg, m);
if(at) {
snprintf(m, sizeof(m), "at '%s': ", at);
(*cfg_parser->err)(cfg_parser->err_arg, m);
}
(*cfg_parser->err)(cfg_parser->err_arg, "error: ");
vsnprintf(m, sizeof(m), fmt, args);
(*cfg_parser->err)(cfg_parser->err_arg, m);
(*cfg_parser->err)(cfg_parser->err_arg, "\n");
return;
}
fprintf(stderr, "%s:%d: ", cfg_parser->filename, cfg_parser->line);
if(at) fprintf(stderr, "at '%s': ", at);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
}
void
c_error(const char *fmt, ...)
{
va_list ap;
int showpos = 0;
if (strcmp(fmt, "syntax error") == 0 || strcmp(fmt, "parse error") == 0) {
showpos = 1;
}
va_start(ap, fmt);
c_error_va_list_pos(showpos, fmt, ap);
va_end(ap);
}
int
c_wrap(void)
{
return 1;
}
struct zone_options*
zone_options_create(region_type* region)
{