File: zone-file-check

package info (click to toggle)
zone-file-check 1.01-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 68 kB
  • ctags: 1
  • sloc: awk: 107; makefile: 50
file content (198 lines) | stat: -rwxr-xr-x 5,025 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/nawk -f
#
# Usage:  zone-file-check file ...
#
# This program checks DNS zone files one by one for syntactic errors.
# Lines containing errors are printed together with the file name and line
# number to the standard output. The program takes one or more filenames
# as arguments and reads the standard input if none are supplied.
#
# Author: Gjermund S�rseth <gjermund@xyzzy.no> 1997
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


function version()	{ return "1.01" }


#########################################################################
#									#
#                 General utility functions				#
#									#
#########################################################################


function is_ip_address(s)  { return s ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ }

function is_name(s)	   { return s ~ /^(@|([\\*A-Za-z0-9.-]+))$/ }

function is_filename(s)	   { return s ~ /^[\\\/A-Za-z0-9._-]+$/ }

function is_number(s)	   { return s ~ /^[0-9]+$/ }

function is_rrtype(s)
{
	return s ~ /^(A|PTR|CNAME|MX|NS|HINFO|TXT|WKS|SOA|RP)$/
}



#########################################################################
#									#
#    Syntax-checking of the RR (Resource Record) type field and		#
#    following data							#
#									#
#########################################################################

function is_rr_a(a, first, last)
{
	return	last - first == 1 && a[first] == "A" &&
		is_ip_address( a[last] )
}

function is_rr_ptr(a, first, last)
{
	return	last - first == 1 && a[first] == "PTR" &&
		is_name( a[last] )
}

function is_rr_cname(a, first, last)
{
	return	last - first == 1 && a[first] == "CNAME" &&
		is_name( a[last] )
}

function is_rr_mx(a, first, last)
{
	return	last - first == 2 && a[first] == "MX" &&
		is_number( a[first+1] ) &&
		is_name( a[last] )
}

function is_rr_ns(a, first, last)
{
	return	last - first == 1 && a[first] == "NS" &&
		is_name( a[last] )
}

function is_rr_hinfo(a, first, last)
{
	return 	last - first == 2 && a[first] == "HINFO" &&
		is_name( a[first+1] ) &&
		is_name( a[first+2] )
}

function is_rr_txt(a, first, last)
{
	return	last - first == 1 && a[first] == "TXT"
}

function is_rr_wks(a, first, last)
{
	return	last - first > 0 && a[first] == "WKS"
}

function is_rr_soa(a, first, last)
{
	return	last - first == 7 && a[first] == "SOA" &&
		is_name( a[first+1] ) &&
		is_name( a[first+2] ) &&
		is_number( a[first+3] ) &&
		is_number( a[first+4] ) &&
		is_number( a[first+5] ) &&
		is_number( a[first+6] ) &&
		is_number( a[first+7] )
}

function is_rr_rp(a, first, last)
{
	return	last - first == 2 && a[first] == "RP" &&
		is_name( a[first+1] ) &&
		is_name( a[last] )
}

#########################################################################
#									#
#        Check a single zone file line for correctness			#
#									#
#########################################################################

function is_rr(a, first, last)
{
	return	is_rr_a(a, first, last)     ||	is_rr_ptr(a, first, last)   ||
		is_rr_cname(a, first, last) ||	is_rr_mx(a, first, last)    ||
		is_rr_ns(a, first, last)    ||	is_rr_hinfo(a, first, last) ||
		is_rr_txt(a, first, last)   ||	is_rr_wks(a, first, last)   ||
		is_rr_soa(a, first, last)
}

function is_special(a, fields)
{
	return	fields >= 2 && a[1] == "$INCLUDE" && is_filename( a[2] ) ||
		fields == 2 && a[1] == "$ORIGIN"  && is_name( a[2] )
}

function is_ok(RR)
{
	gsub( /"[^"]*"/, "-", RR )

	gsub( /\(|\)/, "", RR )

	sub( /[ \t]+(IN|CH|HS)[ \t]+/, " ", RR )

	n = split( RR, a )

	return	is_rrtype(a[1]) && is_rr(a, 1, n) ||

		is_rrtype(a[2]) &&
		(is_name(a[1]) || is_number(a[1])) && is_rr(a, 2, n) ||

		is_rrtype(a[3]) && is_name(a[1]) &&
		is_number(a[2]) && is_rr(a, 3, n) ||

		is_special(a, n)
}

#########################################################################
#									#
#                       Main program					#
#									#
#########################################################################


FNR == 1	{ printf "Checking file \"%s\"\n", FILENAME }

		{ sub( /;.*/, " " ) }

NF == 0		{ next }

		{ ++line

		  RR = (multi > 0) ? RR " " $0 : $0

		  if ( $NF ~ /\)$/ || multi > 0 && line - multi > 10)
			  multi = 0
		  else
		  if ( $NF ~ /\($/ )
			  multi = line

		  if ( !multi && !is_ok(RR) ) {
			  ++err
			  printf "   Line %d:\t%s\n", FNR, RR
		  }
		}

#END		{ printf "==> Total of %d syntax error(s) <==\n", err }