-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathMaster.java
More file actions
457 lines (414 loc) · 12.8 KB
/
Copy pathMaster.java
File metadata and controls
457 lines (414 loc) · 12.8 KB
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* A DNS master file parser. This incrementally parses the file, returning one record at a time.
* When directives are seen, they are added to the state and used when parsing future records.
*
* @author Brian Wellington
*/
public class Master implements AutoCloseable {
private Name origin;
private File file;
private Record last = null;
private long defaultTTL;
private Master included = null;
private final Tokenizer st;
private int currentType;
private int currentDClass;
private long currentTTL;
private boolean needSOATTL;
private Generator generator;
private List<Generator> generators;
private boolean noExpandGenerate;
private boolean noExpandIncludes;
private boolean includeThrowsException;
Master(File file, Name origin, long initialTTL) throws IOException {
if (origin != null && !origin.isAbsolute()) {
throw new RelativeNameException(origin);
}
this.file = file;
st = new Tokenizer(file);
this.origin = origin;
defaultTTL = initialTTL;
}
/**
* Initializes the master file reader and opens the specified master file.
*
* @param filename The master file.
* @param origin The initial origin to append to relative names.
* @param ttl The initial default TTL.
* @throws IOException The master file could not be opened.
*/
public Master(String filename, Name origin, long ttl) throws IOException {
this(new File(filename), origin, ttl);
}
/**
* Initializes the master file reader and opens the specified master file.
*
* @param filename The master file.
* @param origin The initial origin to append to relative names.
* @throws IOException The master file could not be opened.
*/
public Master(String filename, Name origin) throws IOException {
this(new File(filename), origin, -1);
}
/**
* Initializes the master file reader and opens the specified master file.
*
* @param filename The master file.
* @throws IOException The master file could not be opened.
*/
public Master(String filename) throws IOException {
this(new File(filename), null, -1);
}
/**
* Initializes the master file reader.
*
* @param in The input stream containing a master file.
* @param origin The initial origin to append to relative names.
* @param ttl The initial default TTL.
*/
public Master(InputStream in, Name origin, long ttl) {
if (origin != null && !origin.isAbsolute()) {
throw new RelativeNameException(origin);
}
st = new Tokenizer(in);
this.origin = origin;
defaultTTL = ttl;
}
/**
* Initializes the master file reader.
*
* @param in The input stream containing a master file.
* @param origin The initial origin to append to relative names.
*/
public Master(InputStream in, Name origin) {
this(in, origin, -1);
}
/**
* Initializes the master file reader.
*
* @param in The input stream containing a master file.
*/
public Master(InputStream in) {
this(in, null, -1);
}
private Name parseName(String s, Name origin) throws TextParseException {
try {
return Name.fromString(s, origin);
} catch (TextParseException e) {
throw st.exception(e.getMessage());
}
}
private void parseTTLClassAndType() throws IOException {
String s;
boolean seenClass;
// This is a bit messy, since any of the following are legal:
// class ttl type
// ttl class type
// class type
// ttl type
// type
seenClass = false;
s = st.getString();
if ((currentDClass = DClass.value(s)) >= 0) {
s = st.getString();
seenClass = true;
}
currentTTL = -1;
try {
currentTTL = TTL.parseTTL(s);
s = st.getString();
} catch (NumberFormatException e) {
if (defaultTTL >= 0) {
currentTTL = defaultTTL;
} else if (last != null) {
currentTTL = last.getTTL();
}
}
if (!seenClass) {
if ((currentDClass = DClass.value(s)) >= 0) {
s = st.getString();
} else {
currentDClass = DClass.IN;
}
}
if ((currentType = Type.value(s)) < 0) {
throw st.exception("Invalid type '" + s + "'");
}
// BIND allows a missing TTL for the initial SOA record, and uses
// the SOA minimum value. If the SOA is not the first record,
// this is an error.
if (currentTTL < 0) {
if (currentType != Type.SOA) {
throw st.exception("missing TTL");
}
needSOATTL = true;
currentTTL = 0;
}
}
private long parseUInt32(String s) {
if (!Character.isDigit(s.charAt(0))) {
return -1;
}
try {
long l = Long.parseLong(s);
if (!Utils.isUInt32(l)) {
return -1;
}
return l;
} catch (NumberFormatException e) {
return -1;
}
}
private void startGenerate() throws IOException {
String s;
int n;
// The first field is of the form start-end[/step]
// Regexes would be useful here.
s = st.getIdentifier();
n = s.indexOf("-");
if (n < 0) {
throw st.exception("Invalid $GENERATE range specifier: " + s);
}
String startstr = s.substring(0, n);
String endstr = s.substring(n + 1);
String stepstr = null;
n = endstr.indexOf("/");
if (n >= 0) {
stepstr = endstr.substring(n + 1);
endstr = endstr.substring(0, n);
}
long start = parseUInt32(startstr);
long end = parseUInt32(endstr);
long step;
if (stepstr != null) {
step = parseUInt32(stepstr);
} else {
step = 1;
}
if (start < 0 || end < 0 || start > end || step <= 0) {
throw st.exception("Invalid $GENERATE range specifier: " + s);
}
// The next field is the name specification.
String nameSpec = st.getIdentifier();
// Then the ttl/class/type, in the same form as a normal record.
// Only some types are supported.
parseTTLClassAndType();
if (!Generator.supportedType(currentType)) {
throw st.exception("$GENERATE does not support " + Type.string(currentType) + " records");
}
// Next comes the rdata specification.
String rdataSpec = st.getIdentifier();
// That should be the end. However, we don't want to move past the
// line yet, so put back the EOL after reading it.
st.getEOL();
st.unget();
generator =
new Generator(
start, end, step, nameSpec, currentType, currentDClass, currentTTL, rdataSpec, origin);
if (generators == null) {
generators = new ArrayList<>(1);
}
generators.add(generator);
}
private void endGenerate() throws IOException {
// Read the EOL that we put back before.
st.getEOL();
generator = null;
}
private Record nextGenerated() throws IOException {
try {
return generator.nextRecord();
} catch (TextParseException e) {
throw st.exception("Parsing $GENERATE: " + e.getMessage());
}
}
/**
* Returns the next record in the master file. This will process any directives before the next
* record.
*
* @return The next record.
* @throws IOException The master file could not be read, or was syntactically invalid.
*/
private Record nextRecordInternal() throws IOException {
Tokenizer.Token token;
String s;
if (included != null) {
Record rec = included.nextRecord();
if (rec != null) {
return rec;
}
included = null;
}
if (generator != null) {
Record rec = nextGenerated();
if (rec != null) {
return rec;
}
endGenerate();
}
while (true) {
Name name;
token = st.get(true, false);
if (token.type() == Tokenizer.WHITESPACE) {
Tokenizer.Token next = st.get();
if (next.type() == Tokenizer.EOL) {
continue;
} else if (next.type() == Tokenizer.EOF) {
return null;
} else {
st.unget();
}
if (last == null) {
throw st.exception("no owner");
}
name = last.getName();
} else if (token.type() == Tokenizer.EOL) {
continue;
} else if (token.type() == Tokenizer.EOF) {
return null;
} else if (token.value().charAt(0) == '$') {
s = token.value();
if (s.equalsIgnoreCase("$ORIGIN")) {
origin = st.getName(Name.root);
st.getEOL();
continue;
} else if (s.equalsIgnoreCase("$TTL")) {
defaultTTL = st.getTTL();
st.getEOL();
continue;
} else if (s.equalsIgnoreCase("$INCLUDE")) {
if (noExpandIncludes) {
if (includeThrowsException) {
throw st.exception("$INCLUDE encountered, but processing disabled in strict mode");
}
st.getString();
st.getEOL();
continue;
}
String filename = st.getString();
File includeFile = new File(filename);
if (!includeFile.isAbsolute()) {
if (file != null) {
includeFile = new File(file.getParent(), filename);
} else {
throw st.exception("Cannot $INCLUDE using relative path when parsing from stream");
}
}
Name includeOrigin = origin;
token = st.get();
if (token.isString()) {
includeOrigin = parseName(token.value(), Name.root);
st.getEOL();
}
included = new Master(includeFile, includeOrigin, defaultTTL);
/*
* If we continued, we wouldn't be looking in
* the new file. Recursing works better.
*/
return nextRecord();
} else if (s.equalsIgnoreCase("$GENERATE")) {
if (generator != null) {
throw new IllegalStateException("cannot nest $GENERATE");
}
startGenerate();
if (noExpandGenerate) {
endGenerate();
continue;
}
return nextGenerated();
} else {
throw st.exception("Invalid directive: " + s);
}
} else {
s = token.value();
name = parseName(s, origin);
if (last != null && name.equals(last.getName())) {
name = last.getName();
}
}
parseTTLClassAndType();
last = Record.fromString(name, currentType, currentDClass, currentTTL, st, origin);
if (needSOATTL) {
long ttl = ((SOARecord) last).getMinimum();
last.setTTL(ttl);
defaultTTL = ttl;
needSOATTL = false;
}
return last;
}
}
/**
* Returns the next record in the master file. This will process any directives before the next
* record.
*
* @return The next record.
* @throws IOException The master file could not be read, or was syntactically invalid.
*/
public Record nextRecord() throws IOException {
Record rec = null;
try {
rec = nextRecordInternal();
} finally {
if (rec == null) {
st.close();
}
}
return rec;
}
/**
* Disable processing of $INCLUDE directives. When disabled, $INCLUDE statements will not be
* processed. Depending on the contents of the file that would have been included, this may cause
* the zone to be invalid. (e.g. if there is no SOA or NS at the apex)
*/
public void disableIncludes() {
disableIncludes(false);
}
/**
* Disable processing of $INCLUDE directives. When disabled, $INCUDE statements will not be
* processed. Depending on the contents of the file that would have been included, this may cause
* the zone to be invalid. (e.g. if there's no SOA or NS at the apex)
*
* @param strict If true, an exception will be thrown if $INCLUDE is encountered.
*/
public void disableIncludes(boolean strict) {
noExpandIncludes = true;
includeThrowsException = strict;
}
/**
* Specifies whether $GENERATE statements should be expanded. Whether expanded or not, the
* specifications for generated records are available by calling {@link #generators}. This must be
* called before a $GENERATE statement is seen during iteration to have an effect.
*/
public void expandGenerate(boolean wantExpand) {
noExpandGenerate = !wantExpand;
}
/**
* Returns an iterator over the generators specified in the master file; that is, the parsed
* contents of $GENERATE statements.
*
* @see Generator
*/
public Iterator<Generator> generators() {
if (generators != null) {
return Collections.unmodifiableList(generators).iterator();
} else {
return Collections.emptyIterator();
}
}
@Override
public void close() {
if (st != null) {
st.close();
}
}
}