summaryrefslogtreecommitdiffstats
path: root/contrib/bind/bin/named/ns_parser.y
blob: 77dee0b3e437311f44c7a61d1cc71b168a5f18e9 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
%{
#if !defined(lint) && !defined(SABER)
static char rcsid[] = "$Id: ns_parser.y,v 8.11 1997/12/04 07:03:05 halley Exp $";
#endif /* not lint */

/*
 * Copyright (c) 1996, 1997 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

/* Global C stuff goes here. */

#include "port_before.h"

#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>

#include <isc/eventlib.h>
#include <isc/logging.h>

#include "port_after.h"

#include "named.h"
#include "ns_parseutil.h"
#include "ns_lexer.h"

#define SYM_ZONE	0x010000
#define SYM_SERVER	0x020000
#define SYM_KEY		0x030000
#define SYM_ACL		0x040000
#define SYM_CHANNEL	0x050000
#define SYM_PORT	0x060000

#define SYMBOL_TABLE_SIZE 29989		/* should always be prime */
static symbol_table symtab;

#define AUTH_TABLE_SIZE 397		/* should always be prime */
static symbol_table authtab = NULL;

static zone_config current_zone;
static int seen_zone;

static options current_options;
static int seen_options;

static topology_config current_topology;
static int seen_topology;

static server_config current_server;
static int seen_server;

static char *current_algorithm;
static char *current_secret;

static log_config current_logging;
static int current_category;
static int chan_type;
static int chan_level;
static u_int chan_flags;
static int chan_facility;
static char *chan_name;
static int chan_versions;
static u_long chan_max_size;

static log_channel lookup_channel(char *);
static void define_channel(char *, log_channel);
static char *canonical_name(char *);

int yyparse();

%}

%union {
	char *			cp;
	int			s_int;
	long			num;
	u_long			ul_int;
	u_int16_t		us_int;
	struct in_addr		ip_addr;
	ip_match_element	ime;
	ip_match_list		iml;
	key_info		keyi;
	enum axfr_format	axfr_fmt;
}

/* Lexical analyzer return values. */
%token			L_EOS
%token	<ip_addr>	L_IPADDR
%token	<num>		L_NUMBER
%token	<cp>		L_STRING
%token	<cp>		L_QSTRING
%token			L_END_INCLUDE

/* Include support */
%token			T_INCLUDE

/* Items related to the "options" statement: */
%token			T_OPTIONS
%token			T_DIRECTORY T_PIDFILE T_NAMED_XFER
%token			T_DUMP_FILE T_STATS_FILE T_MEMSTATS_FILE
%token			T_FAKE_IQUERY T_RECURSION T_FETCH_GLUE
%token			T_QUERY_SOURCE T_LISTEN_ON T_PORT T_ADDRESS
%type	<us_int>	in_port
%type	<us_int>	maybe_port
%type	<us_int>	maybe_wild_port
%type	<ip_addr>	maybe_wild_addr
%token			T_DATASIZE T_STACKSIZE T_CORESIZE
%token			T_DEFAULT T_UNLIMITED
%token			T_FILES
%token			T_HOSTSTATS T_DEALLOC_ON_EXIT
%token			T_TRANSFERS_IN T_TRANSFERS_OUT T_TRANSFERS_PER_NS
%token			T_TRANSFER_FORMAT T_MAX_TRANSFER_TIME_IN
%token			T_ONE_ANSWER T_MANY_ANSWERS
%type	<axfr_fmt>	transfer_format
%token			T_NOTIFY T_AUTH_NXDOMAIN T_MULTIPLE_CNAMES
%token			T_CLEAN_INTERVAL T_INTERFACE_INTERVAL T_STATS_INTERVAL

/* Items used for the "logging" statement: */
%token			T_LOGGING T_CATEGORY T_CHANNEL T_SEVERITY T_DYNAMIC
%token			T_FILE T_VERSIONS T_SIZE
%token			T_SYSLOG T_DEBUG T_NULL_OUTPUT
%token			T_PRINT_TIME T_PRINT_CATEGORY T_PRINT_SEVERITY
%type	<s_int>		category
%type	<cp>		category_name channel_name facility_name
%type	<s_int>		maybe_syslog_facility

/* Items used for the "topology" statement: */
%token			T_TOPOLOGY

/* ip_match_list */
%type	<ime>		address_match_simple address_match_element address_name
%type	<iml>		address_match_list

/* Items used for "server" statements: */
%token			T_SERVER 
%token			T_LONG_AXFR 
%token			T_BOGUS 
%token			T_TRANSFERS 
%token			T_KEYS

/* Items used for "zone" statements: */
%token			T_ZONE
%type	<num>		optional_class
%type	<s_int>		zone_type
%token			T_IN T_CHAOS T_HESIOD
%token			T_TYPE
%token			T_MASTER T_SLAVE T_STUB T_RESPONSE
%token			T_HINT
%token			T_MASTERS T_TRANSFER_SOURCE
%token			T_ALSO_NOTIFY

/* Items used for access control lists and "allow" clauses: */
%token			T_ACL 
%token	 		T_ALLOW_UPDATE T_ALLOW_QUERY T_ALLOW_TRANSFER

/* Items related to the "key" statement: */
%token			T_SEC_KEY T_ALGID T_SECRET
%type	<keyi>		key_ref
%type  	<cp>		algorithm_id secret

/* Items used for "size_spec" clauses: */
%type	<ul_int>	size_spec

/* Items used for a "check-names" clause: */
%token			T_CHECK_NAMES
%type	<s_int>		check_names_type
%type	<s_int>		check_names_opt
%token			T_WARN T_FAIL T_IGNORE

/* Items used for "forward" clauses: */
%token			T_FORWARD T_FORWARDERS
%token			T_ONLY T_FIRST T_IF_NO_ANSWER T_IF_NO_DOMAIN

/* Items used for yes/no responses: */
%type	<num>		yea_or_nay
%token			T_YES T_TRUE T_NO T_FALSE

/* Miscellaneous items (used in several places): */
%type	<cp>		any_string

%%
config_file: statement_list
	{
		/* nothing */
	}
	;

statement_list: statement
	| statement_list statement
	;

statement: include_stmt
	| options_stmt L_EOS
	| logging_stmt L_EOS
	| server_stmt L_EOS
	| zone_stmt L_EOS
	| acl_stmt L_EOS
	| key_stmt L_EOS
	| L_END_INCLUDE
	| error L_EOS
	| error L_END_INCLUDE
	;

include_stmt: T_INCLUDE L_QSTRING L_EOS	{ lexer_begin_file($2, NULL); }
	;

/*
 * Options
 */

options_stmt: T_OPTIONS 
	{
		if (seen_options)
			parser_error(0, "cannot redefine options");
		current_options = new_options();
	}
	'{' options '}'
	{
		if (!seen_options)
			set_options(current_options, 0);
		else
			free_options(current_options);
		current_options = NULL;
		seen_options = 1;
	}
	;

options: option L_EOS
	| options option L_EOS
	;

option: /* Empty */
	| T_DIRECTORY L_QSTRING
	{
		if (current_options->directory != NULL)
			freestr(current_options->directory);
		current_options->directory = $2;
	}
	| T_NAMED_XFER L_QSTRING
	{
		if (current_options->named_xfer != NULL)
			freestr(current_options->named_xfer);
		current_options->named_xfer = $2;
	}
	| T_PIDFILE L_QSTRING
	{
		if (current_options->pid_filename != NULL)
			freestr(current_options->pid_filename);
		current_options->pid_filename = $2;
	}
	| T_STATS_FILE L_QSTRING
	{
		if (current_options->stats_filename != NULL)
			freestr(current_options->stats_filename);
		current_options->stats_filename = $2;
	}
	| T_MEMSTATS_FILE L_QSTRING
	{
		if (current_options->memstats_filename != NULL)
			freestr(current_options->memstats_filename);
		current_options->memstats_filename = $2;
	}
	| T_DUMP_FILE L_QSTRING
	{
		if (current_options->dump_filename != NULL)
			freestr(current_options->dump_filename);
		current_options->dump_filename = $2;
	}
	| T_FAKE_IQUERY yea_or_nay
	{
		set_boolean_option(current_options, OPTION_FAKE_IQUERY, $2);
	}
	| T_RECURSION yea_or_nay
	{
		set_boolean_option(current_options, OPTION_NORECURSE, !$2);
	}
	| T_FETCH_GLUE yea_or_nay
	{
		set_boolean_option(current_options, OPTION_NOFETCHGLUE, !$2);
	}
	| T_NOTIFY yea_or_nay
	{
		set_boolean_option(current_options, OPTION_NONOTIFY, !$2);
	}
	| T_HOSTSTATS yea_or_nay
	{
		set_boolean_option(current_options, OPTION_HOSTSTATS, $2);
	}
	| T_DEALLOC_ON_EXIT yea_or_nay
	{
		set_boolean_option(current_options, OPTION_DEALLOC_ON_EXIT,
				   $2);
	}
	| T_AUTH_NXDOMAIN yea_or_nay
	{
		set_boolean_option(current_options, OPTION_NONAUTH_NXDOMAIN,
				   !$2);
	}
	| T_MULTIPLE_CNAMES yea_or_nay
	{
		set_boolean_option(current_options, OPTION_MULTIPLE_CNAMES,
				   $2);
	}
	| T_CHECK_NAMES check_names_type check_names_opt
	{
		current_options->check_names[$2] = $3;
	}
	| T_LISTEN_ON maybe_port '{' address_match_list '}'
	{
		char port_string[10];
		symbol_value value;

		(void)sprintf(port_string, "%u", $2);
		if (lookup_symbol(symtab, port_string, SYM_PORT, NULL))
			parser_error(0,
				     "cannot redefine listen-on for port %u",
				     ntohs($2));
		else {
			add_listen_on(current_options, $2, $4);
			value.pointer = NULL;
			define_symbol(symtab, savestr(port_string, 1),
				      SYM_PORT, value, SYMBOL_FREE_KEY);
		}

	}
	| T_FORWARD forward_opt
	| T_FORWARDERS 
	{
		if (current_options->fwdtab) {
			free_forwarders(current_options->fwdtab);
			current_options->fwdtab = NULL;
		}
	}
	'{' opt_forwarders_list '}'
	| T_QUERY_SOURCE query_source
	| T_ALLOW_QUERY '{' address_match_list '}'
	{
		if (current_options->query_acl)
			free_ip_match_list(current_options->query_acl);
		current_options->query_acl = $3;
	}
	| T_ALLOW_TRANSFER '{' address_match_list '}'
	{
		if (current_options->transfer_acl)
			free_ip_match_list(current_options->transfer_acl);
		current_options->transfer_acl = $3;
	}
	| T_TOPOLOGY '{' address_match_list '}'
	{
		if (current_options->topology)
			free_ip_match_list(current_options->topology);
		current_options->topology = $3;
	}
	| size_clause
	{
		/* To get around the $$ = $1 default rule. */
	}
	| transfer_clause
	| T_TRANSFER_FORMAT transfer_format
	{
		current_options->transfer_format = $2;
	}
	| T_MAX_TRANSFER_TIME_IN L_NUMBER
	{
		current_options->max_transfer_time_in = $2 * 60;
	}
	| T_CLEAN_INTERVAL L_NUMBER
	{
		current_options->clean_interval = $2 * 60;
	}
	| T_INTERFACE_INTERVAL L_NUMBER
	{
		current_options->interface_interval = $2 * 60;
	}
	| T_STATS_INTERVAL L_NUMBER
	{
		current_options->stats_interval = $2 * 60;
	}
	| error
	;

transfer_format: T_ONE_ANSWER
	{
		$$ = axfr_one_answer;
	}
	| T_MANY_ANSWERS
	{
		$$ = axfr_many_answers;
	}
	;
	
maybe_wild_addr: L_IPADDR { $$ = $1; }
	| '*' { $$.s_addr = htonl(INADDR_ANY); }
	;

maybe_wild_port: in_port { $$ = $1; }
	| '*' { $$ = htons(0); }
	;

query_source_address: T_ADDRESS maybe_wild_addr
	{
		current_options->query_source.sin_addr = $2;
	}
	;

query_source_port: T_PORT maybe_wild_port
	{
		current_options->query_source.sin_port = $2;
	}
	;

query_source: query_source_address
	| query_source_port
	| query_source_address query_source_port
	| query_source_port query_source_address
	;

maybe_port: /* nothing */ { $$ = htons(NS_DEFAULTPORT); }
	| T_PORT in_port { $$ = $2; }
	;

yea_or_nay: T_YES
	{ 
		$$ = 1;	
	}
	| T_TRUE
	{ 
		$$ = 1;	
	}
	| T_NO
	{ 
		$$ = 0;	
	}
	| T_FALSE 
	{ 
		$$ = 0;	
	}
	| L_NUMBER
	{ 
		if ($1 == 1 || $1 == 0) {
			$$ = $1;
		} else {
			parser_warning(0,
				       "number should be 0 or 1; assuming 1");
			$$ = 1;
		}
	}
	;

check_names_type: T_MASTER
	{
		$$ = primary_trans;
	}
	| T_SLAVE
	{
		$$ = secondary_trans;
	}
	| T_RESPONSE
	{
		$$ = response_trans;
	}
	;

check_names_opt: T_WARN
	{
		$$ = warn;
	}
	| T_FAIL
	{
		$$ = fail;
	}
	| T_IGNORE
	{
		$$ = ignore;
	}
	;

forward_opt: T_ONLY
	{
		set_boolean_option(current_options, OPTION_FORWARD_ONLY, 1);
	}
	| T_FIRST
	{
		set_boolean_option(current_options, OPTION_FORWARD_ONLY, 0);
	}
	| T_IF_NO_ANSWER
	{
		parser_warning(0, "forward if-no-answer is unimplemented");
	}
	| T_IF_NO_DOMAIN
	{
		parser_warning(0, "forward if-no-domain is unimplemented");
	}
	;

size_clause: T_DATASIZE size_spec
	{
		current_options->data_size = $2;
	}
	| T_STACKSIZE size_spec
	{
		current_options->stack_size = $2;
	}
	| T_CORESIZE size_spec
	{
		current_options->core_size = $2;
	}
	| T_FILES size_spec
	{
		current_options->files = $2;
	}
	;

size_spec: any_string
	{
		u_long result;

		if (unit_to_ulong($1, &result))
			$$ = result;
		else {
			parser_error(0, "invalid unit string '%s'", $1);
			/* 0 means "use default" */
			$$ = 0;
		}
		freestr($1);
	}
	| L_NUMBER
	{	
		$$ = (u_long)$1;
	}
	| T_DEFAULT
	{
		$$ = 0;
	}
	| T_UNLIMITED
	{
		$$ = ULONG_MAX;
	}
	;

transfer_clause: T_TRANSFERS_IN L_NUMBER
	{
		current_options->transfers_in = (u_long) $2;
	}
	| T_TRANSFERS_OUT L_NUMBER
	{
		current_options->transfers_out = (u_long) $2;
	}
	| T_TRANSFERS_PER_NS L_NUMBER
	{
		current_options->transfers_per_ns = (u_long) $2;
	}
	;

opt_forwarders_list: /* nothing */
	| forwarders_in_addr_list
	;

forwarders_in_addr_list: forwarders_in_addr L_EOS
	{
		/* nothing */
	}
	| forwarders_in_addr_list forwarders_in_addr L_EOS
	{
		/* nothing */
	}
	;

forwarders_in_addr: L_IPADDR
	{
	  	add_forwarder(current_options, $1);
	}
	;

/*
 * Logging
 */

logging_stmt: T_LOGGING
	{
		current_logging = begin_logging();
	}
	'{' logging_opts_list '}'
	{
		end_logging(current_logging, 1);
		current_logging = NULL;
	}
	;

logging_opts_list: logging_opt L_EOS
	| logging_opts_list logging_opt L_EOS
	| error
	;

logging_opt: T_CATEGORY category 
	{
		current_category = $2;
	}
	'{' channel_list '}'
	| T_CHANNEL channel_name
	{
		chan_type = log_null;
		chan_flags = 0;
		chan_level = log_info;
	}
	'{' channel_opt_list '}'
	{
		log_channel current_channel = NULL;

		if (lookup_channel($2) != NULL) {
			parser_error(0, "can't redefine channel '%s'", $2);
			freestr($2);
		} else {
			switch (chan_type) {
			case log_file:
				current_channel =
					log_new_file_channel(chan_flags,
							     chan_level,
							     chan_name, NULL,
							     chan_versions,
							     chan_max_size);
				freestr(chan_name);
				chan_name = NULL;
				break;
			case log_syslog:
				current_channel =
					log_new_syslog_channel(chan_flags,
							       chan_level,
							       chan_facility);
				break;
			case log_null:
				current_channel = log_new_null_channel();
				break;
			default:
				ns_panic(ns_log_parser, 1,
					 "unknown channel type: %d",
					 chan_type);
			}
			if (current_channel == NULL)
				ns_panic(ns_log_parser, 0,
					 "couldn't create channel");
			define_channel($2, current_channel);
		}
	}
	;

channel_severity: any_string
	{
		symbol_value value;

		if (lookup_symbol(constants, $1, SYM_LOGGING, &value)) {
			chan_level = value.integer;
		} else {
			parser_error(0, "unknown severity '%s'", $1);
			chan_level = log_debug(99);
		}
		freestr($1);
	}
	| T_DEBUG
	{
		chan_level = log_debug(1);
	}
	| T_DEBUG L_NUMBER
	{
		chan_level = $2;
	}
	| T_DYNAMIC
	{
		chan_level = 0;
		chan_flags |= LOG_USE_CONTEXT_LEVEL|LOG_REQUIRE_DEBUG;
	}
	;

version_modifier: T_VERSIONS L_NUMBER
	{
		chan_versions = $2;
		chan_flags |= LOG_TRUNCATE;
	}
	| T_VERSIONS T_UNLIMITED
	{
		chan_versions = LOG_MAX_VERSIONS;
		chan_flags |= LOG_TRUNCATE;
	}
	;

size_modifier: T_SIZE size_spec
	{
		chan_max_size = $2;
	}
	;

maybe_file_modifiers: /* nothing */
	{
		chan_versions = 0;
		chan_max_size = ULONG_MAX;
	}
	| version_modifier
	{
		chan_max_size = ULONG_MAX;
	}
	| size_modifier
	{
		chan_versions = 0;
	}
	| version_modifier size_modifier
	| size_modifier version_modifier
	;

channel_file: T_FILE L_QSTRING maybe_file_modifiers
	{
		chan_flags |= LOG_CLOSE_STREAM;
		chan_type = log_file;
		chan_name = $2;
	}
	;


facility_name: any_string { $$ = $1; }
	| T_SYSLOG { $$ = savestr("syslog", 1); }
	;

maybe_syslog_facility: /* nothing */ { $$ = LOG_DAEMON; }
	| facility_name
	{
		symbol_value value;

		if (lookup_symbol(constants, $1, SYM_SYSLOG, &value)) {
			$$ = value.integer;
		} else {
			parser_error(0, "unknown facility '%s'", $1);
			$$ = LOG_DAEMON;
		}
		freestr($1);
	}
	;

channel_syslog: T_SYSLOG maybe_syslog_facility
	{
		chan_type = log_syslog;
		chan_facility = $2;
	}
	;

channel_opt: channel_file { /* nothing to do */ }
	| channel_syslog { /* nothing to do */ }
	| T_NULL_OUTPUT
	{
		chan_type = log_null;
	}
	| T_SEVERITY channel_severity { /* nothing to do */ }
	| T_PRINT_TIME yea_or_nay
	{
		if ($2)
			chan_flags |= LOG_TIMESTAMP;
		else
			chan_flags &= ~LOG_TIMESTAMP;
	}
	| T_PRINT_CATEGORY yea_or_nay
	{
		if ($2)
			chan_flags |= LOG_PRINT_CATEGORY;
		else
			chan_flags &= ~LOG_PRINT_CATEGORY;
	}
	| T_PRINT_SEVERITY yea_or_nay
	{
		if ($2)
			chan_flags |= LOG_PRINT_LEVEL;
		else
			chan_flags &= ~LOG_PRINT_LEVEL;
	}
	;

channel_opt_list: channel_opt L_EOS
	| channel_opt_list channel_opt L_EOS
	| error
	;

channel_name: any_string
	| T_NULL_OUTPUT { $$ = savestr("null", 1); }
	;

channel: channel_name
	{
		log_channel channel;
		symbol_value value;

		if (current_category >= 0) {
			channel = lookup_channel($1);
			if (channel != NULL) {
				add_log_channel(current_logging,
						current_category, channel);
			} else
				parser_error(0, "unknown channel '%s'", $1);
		}
		freestr($1);
	}
	;

channel_list: channel L_EOS
	| channel_list channel L_EOS
	| error
	;

category_name: any_string
	| T_DEFAULT { $$ = savestr("default", 1); }
	| T_NOTIFY { $$ = savestr("notify", 1); }
	;

category: category_name
	{
		symbol_value value;

		if (lookup_symbol(constants, $1, SYM_CATEGORY, &value))
			$$ = value.integer;
		else {
			parser_error(0, "invalid logging category '%s'",
				     $1);
			$$ = -1;
		}
		freestr($1);
	}
	;

/*
 * Server Information
 */

server_stmt: T_SERVER L_IPADDR
	{
		char *ip_printable;
		symbol_value value;
		
		ip_printable = inet_ntoa($2);
		value.pointer = NULL;
		if (lookup_symbol(symtab, ip_printable, SYM_SERVER, NULL))
			seen_server = 1;
		else
			seen_server = 0;
		if (seen_server)
			parser_error(0, "cannot redefine server '%s'", 
				     ip_printable);
		else
			define_symbol(symtab, savestr(ip_printable, 1),
				      SYM_SERVER, value,
				      SYMBOL_FREE_KEY);
		current_server = begin_server($2);
	}
	'{' server_info_list '}'
	{
		end_server(current_server, !seen_server);
	}
	;

server_info_list: server_info L_EOS
	| server_info_list server_info L_EOS
	;

server_info: T_BOGUS yea_or_nay
	{
		set_server_option(current_server, SERVER_INFO_BOGUS, $2);
	}
	| T_TRANSFERS L_NUMBER
	{
		set_server_transfers(current_server, (int)$2);
	}
	| T_TRANSFER_FORMAT transfer_format
	{
		set_server_transfer_format(current_server, $2);
	}
	| T_KEYS '{' key_list '}'
	| error
	;

/*
 * Address Matching
 */

address_match_list: address_match_element L_EOS
	{
		ip_match_list iml;
		
		iml = new_ip_match_list();
		if ($1 != NULL)
			add_to_ip_match_list(iml, $1);
		$$ = iml;
	}
	| address_match_list address_match_element L_EOS
	{
		if ($2 != NULL)
			add_to_ip_match_list($1, $2);
		$$ = $1;
	}
	;

address_match_element: address_match_simple
	| '!' address_match_simple
	{
		if ($2 != NULL)
			ip_match_negate($2);
		$$ = $2;
	}
	;

address_match_simple: L_IPADDR
	{
		$$ = new_ip_match_pattern($1, 32);
	}
	| L_IPADDR '/' L_NUMBER
	{
		if ($3 < 0 || $3 > 32) {
			parser_error(0, "mask bits out of range; skipping");
			$$ = NULL;
		} else {
			$$ = new_ip_match_pattern($1, $3);
			if ($$ == NULL)
				parser_error(0, 
					   "address/mask mismatch; skipping");
		}
	}
	| L_NUMBER '/' L_NUMBER
	{
		struct in_addr ia;

		if ($1 > 255) {
			parser_error(0, "address out of range; skipping");
			$$ = NULL;
		} else {
			if ($3 < 0 || $3 > 32) {
				parser_error(0,
					"mask bits out of range; skipping");
					$$ = NULL;
			} else {
				ia.s_addr = htonl(($1 & 0xff) << 24);
				$$ = new_ip_match_pattern(ia, $3);
				if ($$ == NULL)
					parser_error(0, 
					   "address/mask mismatch; skipping");
			}
		}
	}
	| address_name
	| '{' address_match_list '}'
	{
		char name[256];

		/*
		 * We want to be able to clean up this iml later so
		 * we give it a name and treat it like any other acl.
		 */
		sprintf(name, "__internal_%p", $2);
		define_acl(savestr(name, 1), $2);
  		$$ = new_ip_match_indirect($2);
	}
	;

address_name: any_string
	{
		ip_match_list iml;

		iml = lookup_acl($1);
		if (iml == NULL) {
			parser_error(0, "unknown ACL '%s'", $1);
			$$ = NULL;
		} else
			$$ = new_ip_match_indirect(iml);
		freestr($1);
	}
	;

/*
 * Keys
 */

key_ref: any_string
	{
		key_info ki;

		ki = lookup_key($1);
		if (ki == NULL) {
			parser_error(0, "unknown key '%s'", $1);
			$$ = NULL;
		} else
			$$ = ki;
		freestr($1);
	}
	;

key_list_element: key_ref
	{
		if ($1 == NULL)
			parser_error(0, "empty key not added to server list ");
		else
			add_server_key_info(current_server, $1);
	}
	;

key_list: key_list_element L_EOS
	| key_list key_list_element L_EOS
	| error
	;

key_stmt: T_SEC_KEY
	{
		current_algorithm = NULL;
		current_secret = NULL;
	}
	any_string '{' key_definition '}'
	{
		key_info ki;

		if (lookup_key($3) != NULL) {
			parser_error(0, "can't redefine key '%s'", $3);
			freestr($3);
		} else {
			if (current_algorithm == NULL ||
			    current_secret == NULL)
				parser_error(0, "skipping bad key '%s'", $3);
			else {
				ki = new_key_info($3, current_algorithm,
						  current_secret);
				define_key($3, ki);
			}
		}
	}
	;
	
key_definition: algorithm_id secret
	{
		current_algorithm = $1;
		current_secret = $2;
	}
	| secret algorithm_id
	{
		current_algorithm = $2;
		current_secret = $1;
	}
	| error
	{
		current_algorithm = NULL;
		current_secret = NULL;
	}
	;

algorithm_id: T_ALGID any_string L_EOS { $$ = $2; }
	;

secret: T_SECRET any_string L_EOS { $$ = $2; }
	;

/*
 * ACLs
 */

acl_stmt: T_ACL any_string '{' address_match_list '}'
	{
		if (lookup_acl($2) != NULL) {
			parser_error(0, "can't redefine ACL '%s'", $2);
			freestr($2);
		} else
			define_acl($2, $4);
	}
	;

/*
 * Zones
 */
	
zone_stmt: T_ZONE L_QSTRING optional_class
	{
		int sym_type;
		symbol_value value;
		char *zone_name;

		if (!seen_options)
			parser_error(0,
             "no options statement before first zone; using previous/default");
		sym_type = SYM_ZONE | ($3 & 0xffff);
		value.pointer = NULL;
		zone_name = canonical_name($2);
		if (zone_name == NULL) {
			parser_error(0, "can't make zone name '%s' canonical",
				     $2);
			seen_zone = 1;
			zone_name = savestr("__bad_zone__", 1);
		} else {
			seen_zone = lookup_symbol(symtab, zone_name, sym_type,
						  NULL);
			if (seen_zone) {
				parser_error(0,
					"cannot redefine zone '%s' class %d",
					     zone_name, $3);
			} else
				define_symbol(symtab, zone_name, sym_type,
					      value, 0);
		}
		freestr($2);
		current_zone = begin_zone(zone_name, $3); 
	}
	optional_zone_options_list
	{ end_zone(current_zone, !seen_zone); }
	;

optional_zone_options_list: /* Empty */
	| '{' zone_option_list '}'
	;

optional_class: /* Empty */
	{
		$$ = C_IN;
	}
	| any_string
	{
		symbol_value value;

		if (lookup_symbol(constants, $1, SYM_CLASS, &value))
			$$ = value.integer;
		else {
			/* the zone validator will give the error */
			$$ = C_NONE;
		}
		freestr($1);
	}
	;

zone_type: T_MASTER
	{
		$$ = Z_MASTER;
	}
	| T_SLAVE
	{
		$$ = Z_SLAVE;
	}
	| T_HINT
	{
		$$ = Z_HINT;
	}
	| T_STUB
	{
		$$ = Z_STUB;
	}
	;

zone_option_list: zone_option L_EOS
	| zone_option_list zone_option L_EOS
	;

zone_option: T_TYPE zone_type
	{
		if (!set_zone_type(current_zone, $2))
			parser_warning(0, "zone type already set; skipping");
	}
	| T_FILE L_QSTRING
	{
		if (!set_zone_filename(current_zone, $2))
			parser_warning(0,
				       "zone filename already set; skipping");
	}
	| T_MASTERS '{' master_in_addr_list '}'
	| T_TRANSFER_SOURCE maybe_wild_addr
	{
		set_zone_transfer_source(current_zone, $2);
	}
	| T_CHECK_NAMES check_names_opt
	{
		if (!set_zone_checknames(current_zone, $2))
			parser_warning(0,
	                              "zone checknames already set; skipping");
	}
	| T_ALLOW_UPDATE '{' address_match_list '}'
	{
		if (!set_zone_update_acl(current_zone, $3))
			parser_warning(0,
				      "zone update acl already set; skipping");
	}
	| T_ALLOW_QUERY '{' address_match_list '}'
	{
		if (!set_zone_query_acl(current_zone, $3))
			parser_warning(0,
				      "zone query acl already set; skipping");
	}
	| T_ALLOW_TRANSFER '{' address_match_list '}'
	{
		if (!set_zone_transfer_acl(current_zone, $3))
			parser_warning(0,
				    "zone transfer acl already set; skipping");
	}
	| T_MAX_TRANSFER_TIME_IN L_NUMBER
	{
		if (!set_zone_transfer_time_in(current_zone, $2*60))
			parser_warning(0,
		       "zone max transfer time (in) already set; skipping");
	}
	| T_NOTIFY yea_or_nay
	{
		set_zone_notify(current_zone, $2);
	}
	| T_ALSO_NOTIFY '{' opt_notify_in_addr_list '}'
	| error
	;

master_in_addr_list: master_in_addr L_EOS
	{
		/* nothing */
	}
	| master_in_addr_list master_in_addr L_EOS
	{
		/* nothing */
	}
	;

master_in_addr: L_IPADDR
	{
	  	add_zone_master(current_zone, $1);
	}
	;

opt_notify_in_addr_list: /* nothing */
	| notify_in_addr_list
	;

notify_in_addr_list: notify_in_addr L_EOS
	{
		/* nothing */
	}
	| notify_in_addr_list notify_in_addr L_EOS
	{
		/* nothing */
	}
	;

notify_in_addr: L_IPADDR
	{
	  	add_zone_notify(current_zone, $1);
	}
	;

/*
 * Misc.
 */

in_port: L_NUMBER
	{
		if ($1 < 0 || $1 > 65535) {
		  	parser_warning(0, 
			  "invalid IP port number '%d'; setting port to 0",
			               $1);
			$1 = 0;
		} else
			$$ = htons($1);
	}
	;

any_string: L_STRING
	| L_QSTRING
	;
	
%%

static char *
canonical_name(char *name) {
	char canonical[MAXDNAME];
	
	if (strlen(name) >= MAXDNAME)
		return (NULL);
	strcpy(canonical, name);
	if (makename(canonical, ".", sizeof canonical) < 0)
		return (NULL);
	return (savestr(canonical, 0));
}

static void
init_acls() {
	ip_match_element ime;
	ip_match_list iml;
	struct in_addr address;

	/* Create the predefined ACLs */

	address.s_addr = 0U;

	/* ACL "any" */
	ime = new_ip_match_pattern(address, 0);
	iml = new_ip_match_list();
	add_to_ip_match_list(iml, ime);
	define_acl(savestr("any", 1), iml);

	/* ACL "none" */
	ime = new_ip_match_pattern(address, 0);
	ip_match_negate(ime);
	iml = new_ip_match_list();
	add_to_ip_match_list(iml, ime);
	define_acl(savestr("none", 1), iml);

	/* ACL "localhost" */
	ime = new_ip_match_localhost();
	iml = new_ip_match_list();
	add_to_ip_match_list(iml, ime);
	define_acl(savestr("localhost", 1), iml);

	/* ACL "localnets" */
	ime = new_ip_match_localnets();
	iml = new_ip_match_list();
	add_to_ip_match_list(iml, ime);
	define_acl(savestr("localnets", 1), iml);
}

static void
free_sym_value(int type, void *value) {
	ns_debug(ns_log_parser, 99, "free_sym_value: type %06x value %p",
		 type, value);
	type &= ~0xffff;
	switch (type) {
	case SYM_ACL:
		free_ip_match_list(value);
		break;
	case SYM_KEY:
		free_key_info(value);
		break;
	default:
		ns_panic(ns_log_parser, 1,
			 "unhandled case in free_sym_value()");
		/* NOTREACHED */
		break;
	}
}

static log_channel
lookup_channel(char *name) {
	symbol_value value;

	if (lookup_symbol(symtab, name, SYM_CHANNEL, &value))
		return ((log_channel)(value.pointer));
	return (NULL);
}

static void
define_channel(char *name, log_channel channel) {
	symbol_value value;

	value.pointer = channel;  
	define_symbol(symtab, name, SYM_CHANNEL, value, SYMBOL_FREE_KEY);
}

static void
define_builtin_channels() {
	define_channel(savestr("default_syslog", 1), syslog_channel);
	define_channel(savestr("default_debug", 1), debug_channel);
	define_channel(savestr("default_stderr", 1), stderr_channel);
	define_channel(savestr("null", 1), null_channel);
}

static void
parser_setup() {
	seen_options = 0;
	seen_topology = 0;
	symtab = new_symbol_table(SYMBOL_TABLE_SIZE, NULL);
	if (authtab != NULL)
		free_symbol_table(authtab);
	authtab = new_symbol_table(AUTH_TABLE_SIZE, free_sym_value);
	init_acls();
	define_builtin_channels();
}

static void
parser_cleanup() {
	if (symtab != NULL)
		free_symbol_table(symtab);
	symtab = NULL;
	/*
	 * We don't clean up authtab here because the ip_match_lists are in
	 * use.
	 */
}

/*
 * Public Interface
 */

ip_match_list
lookup_acl(char *name) {
	symbol_value value;

	if (lookup_symbol(authtab, name, SYM_ACL, &value))
		return ((ip_match_list)(value.pointer));
	return (NULL);
}

void
define_acl(char *name, ip_match_list iml) {
	symbol_value value;

	INSIST(name != NULL);
	INSIST(iml != NULL);

	value.pointer = iml;
	define_symbol(authtab, name, SYM_ACL, value,
		      SYMBOL_FREE_KEY|SYMBOL_FREE_VALUE);
	ns_debug(ns_log_parser, 7, "acl %s", name);
	dprint_ip_match_list(ns_log_parser, iml, 2, "allow ", "deny ");
}

key_info
lookup_key(char *name) {
	symbol_value value;

	if (lookup_symbol(authtab, name, SYM_KEY, &value))
		return ((key_info)(value.pointer));
	return (NULL);
}

void
define_key(char *name, key_info ki) {
	symbol_value value;

	INSIST(name != NULL);
	INSIST(ki != NULL);

	value.pointer = ki;
	define_symbol(authtab, name, SYM_KEY, value, SYMBOL_FREE_VALUE);
	dprint_key_info(ki);
}

void
parse_configuration(const char *filename) {
	FILE *config_stream;

	config_stream = fopen(filename, "r");
	if (config_stream == NULL)
		ns_panic(ns_log_parser, 0, "can't open '%s'", filename);

	lexer_setup();
	parser_setup();
	lexer_begin_file(filename, config_stream);
	(void)yyparse();
	lexer_end_file();
	parser_cleanup();
}

void
parser_initialize(void) {
	lexer_initialize();
}

void
parser_shutdown(void) {
	if (authtab != NULL)
		free_symbol_table(authtab);
	lexer_shutdown();
}
OpenPOWER on IntegriCloud