File: tachk

package info (click to toggle)
dnssec-tools 1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 16,064 kB
  • sloc: perl: 44,399; ansic: 31,547; cpp: 21,306; sh: 15,813; xml: 2,113; makefile: 1,390; pascal: 836; python: 290; csh: 11
file content (481 lines) | stat: -rwxr-xr-x 9,362 bytes parent folder | download
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
#!/usr/bin/perl
#
# Copyright 2005-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# tachk
#
#	This script checks the validity of configured trust anchors
#

use strict;

use Getopt::Long qw(:config no_ignore_case_always);


use Net::DNS;
use Net::DNS::SEC::Tools::conf;
use Net::DNS::SEC::Tools::tooloptions;
use Net::DNS::SEC::Tools::BootStrap;

#
# Version information.
#
my $NAME   = "tachk";
my $VERS   = "$NAME version: 1.13.0";
my $DTVERS = "DNSSEC-Tools Version: 1.13";

######################################################################
# detect needed perl module requirements
#
dnssec_tools_load_mods('Net::DNS::SEC' => "");

#######################################################################

#
# Data required for command line options.
#
my %options = ();			# Filled option array.
my @opts =
(
	"valid",			# List valid trust anchors
	"invalid",			# List invalid trust anchors

	"count",			# Only give a count of trust anchors
	"terse",			# Give terse output.
	"Version",			# Display version.
	"long",				# Give long output.

	"help",				# Give a usage message and exit.
);

#
# Configuration options.
#
my %config = ();

#
# Flag values for the various options.  Variable/option connection should
# be obvious.
#
my $validflag;
my $invalidflag;

my $cntflag;
my $terse;
my $long;
my $version;

my $count   = 0;			# Record-match count.

my %anchors = ();

#######################################################################

my $ret;				# Return code from main().

$ret = main();
exit($ret);

#-----------------------------------------------------------------------------
#
# Routine:	main()
#
sub main()
{
	my $argc = @ARGV;		# Number of command line arguments.
	my $errors = 0;			# Total error count.

	#
	# Check our options.
	#
	doopts($argc);

	#
	# Parse the conf file.
	#
	%config = parseconfig();

	#
	# Read the named.conf file.
	#
	getanchors($ARGV[0]);

	#
	# Give the output.
	#
	showanchors();

	#
	# If the matching-record count should be given, give the count in
	# requested format.
	#
	if($cntflag)
	{
		if($terse)
		{
			print "$count\n";
		}
		else
		{
			my $plural = "s";
			$plural = "" if($count == 1);

			print "$count matching record$plural\n";
		}
	}
	return(0);
}

#-----------------------------------------------------------------------------
#
# Routine:	doopts()
#
# Purpose:	This routine shakes and bakes our command line options.
#		A bunch of option variables are set according to the specified
#		options.  Then a little massaging is done to make sure that
#		the proper actions are taken.  A few options imply others, so
#		the implied options are set if the implying options are given.
#
sub doopts
{
	my $argc = shift;			# Command line argument count.

	#
	# Parse the options.
	#
	GetOptions(\%options,@opts) || usage();

	#
	# Set our option variables based on the parsed options.
	#
	$validflag	= $options{'valid'}	|| 0;
	$invalidflag	= $options{'invalid'}	|| 0;

	$cntflag	= $options{'count'}	|| 0;
	$terse		= $options{'terse'}	|| 0;
	$long		= $options{'long'}	|| 0;
	$version	= $options{'Version'}	|| 0;

	#
	# Display the version number
	#
	show_version() if ($options{'Version'});

	#
	# Give a usage flag if asked.
	#
	usage() if(defined($options{'help'}));

	#
	# Ensure we were given a named.conf file to check.
	#
	$argc = @ARGV;
	if($argc == 0)
	{
		usage();
		exit(1);
	}
}

#-----------------------------------------------------------------------------
#
# Routine:  getanchors()
#
sub getanchors
{
	my $conffile = shift;		# named.conf file
	my $inblock = 0;

	#
	# Make sure the config file actually exists. If not, we'll quietly return.
	#
	return if(! -e $conffile);

	#
	# Open up the config file.
	#
	if (open (CONF,"< $conffile") == 0)
	{
		print STDERR "unable to open $conffile\n";
		return;
	}

	#
	# Read each line from the file, build a DNSKEY RR, and add it
	# to the anchors hash table.
	#
	while (<CONF>)
	{
		#
		# Do the in-block stuff first.
		#
		if ($inblock)
		{
			#
			# If this is the end of the block, we're done.
			#
			if (/\}/)
			{
				$inblock = 0;
			}
			#
			# Read the next line and build a DNSKEY RR.
			#
			else
			{
				my ($name, $flags, $protocol, $algorithm, $keydata) = split(/ /, $_, 5);
				my $rrstring = substr($name,2,-2) . " IN DNSKEY " . $flags . " " . $protocol . " " . $algorithm . " " . substr($keydata,2,-3);

				my $dnskeyrr = Net::DNS::RR->new($rrstring);

				#
				# Add this variable/value pair to the anchors hash table.
				#
				$anchors{$dnskeyrr->keytag} = {
					name		=> $dnskeyrr->name,
					keytag	=> $dnskeyrr->keytag,
					valid		=> 0,
					dnskey	=> $dnskeyrr
				};
			}
		}
		#
		# If we're not in a block yet, see if this line indicates the start of one.
		#
		if (/trusted-keys/)
		{
			# I need to check on the open brace as well, this will suffice for now.
			$inblock = 1;
		}
	}

	#
	# Close the configuration file
	#
	close(CONF);
}

#-----------------------------------------------------------------------------
#
# Routine:  showanchors()
#
sub showanchors
{
	#
	# Get a Net::DNS resolver to make queries through.
	#
	my $res = Net::DNS::Resolver->new;
	my %anchor = ();

	#
	# Iterate over each of the trust anchors.
	#
	foreach my $k (sort(keys(%anchors)))
	{
		%anchor = %{$anchors{$k}};

		#
		# Start by querying for the name and any DNSKEY RRs.
		#
		my $packet = $res->send($anchor{'name'},'DNSKEY');

		#
		# If there are no DNSKEY RRs for the name.
		#
		if ($packet->header->ancount == 0)
		{
			#
			# Mark the trust anchor as invalid.
			#
			$anchor{'valid'} = 0;

			#
			# If the user is interested in invalid trust anchors.
			#
			if ($invalidflag) {
				#
				# Bump the matching-records count.
				#
				$count++;

				#
				# If the user is not just interested in the count of matching records.
				#
				if (!$cntflag)
				{
					#
					# Give the output line appropriate
					#
					print "$anchor{'name'}/$anchor{'keytag'} is invalid.\n";
				}
			}
		}
		#
		# There are DNSKEY RRs for the name.
		#
		else
		{
			#
			# Iterate over the RRs returned in the answer.
			#
			foreach my $rr ($packet->answer)
			{
				#
				# If one of the RRs has a keytag that matches our anchor.
				#
				if ($anchor{'keytag'} == $rr->keytag) {
					#
					# Mark the anchor as valid.
					#
					$anchor{'valid'} = 1;
				}
			}

			#
			# If the user is interested in valid trust anchors.
			#
			if ($validflag) {
				#
				# Bump the matching-records count.
				#
				$count++;

				#
				# If the user is not just interested in the count of matching records.
				#
				if (!$cntflag)
				{
					#
					# Give the output line appropriate.
					#
					print "$anchor{'name'}/$anchor{'keytag'} is valid.\n";
				}
			}
		}
	}
}

#----------------------------------------------------------------------
#
# Routine:	show_version()
#
# Purpose:	Print the version number(s) and exit.
#
sub show_version
{
	print STDERR "$VERS\n";
	print STDERR "$DTVERS\n";
	exit(0);
}

#-----------------------------------------------------------------------------
#
# Routine:	usage()
#
sub usage
{
	print STDERR "usage:  tachk [options] <keyrec-file>\n";
	print STDERR "\trecord-attribute options:\n";
	print STDERR "\t\t-valid\t	show valid trust anchors\n";
	print STDERR "\t\t-invalid	show invalid trust anchors\n";
	print STDERR "\toutput-format options:\n";
	print STDERR "\t\t-count		only give count of matching trust anchors\n";
	print STDERR "\t\t-terse\t	terse output\n";
	print STDERR "\t\t-long		long output\n";
	print STDERR "\t\t-Version	version message \n";
	print STDERR "\t\t-help		help message \n";
	exit(0);
}

1;

##############################################################################
#

=pod

=head1 NAME

tachk - Check the validity of the trust anchors in a B<named.conf> file

=head1 SYNOPSIS

  tachk [options] <named.conf>

=head1 DESCRIPTION

B<tachk> checks the validity of the trust anchors in the specified
B<named.conf> file.  The output given depends on the options selected.

Note:  This script may be removed in future releases.

=head1 OPTIONS

B<tachk> takes two types of options:  record-attribute options
and output-style options.  These option sets are detailed below.

=head2 Record-Attribute Options

These options define which trust anchor records will be displayed.

=over 4

=item B<-valid>

This option displays the valid trust anchors in a B<named.conf> file.

=item B<-invalid>

This option displays the invalid trust anchors in a B<named.conf> file.

=back

=head2 Output-Format Options

These options define how the trust anchor information will be displayed.
Without any of these options, the zone name and key tag will be displayed
for each trust anchor.

=over 4

=item B<-count>

The count of matching records will be displayed, but the matching records
will not be.

=item B<-long>

The long form of output will be given.  The zone name and key tag will be
displayed for each trust anchor.

=item B<-terse>

This option displays only the name of the zones selected by other options.

=item B<-Version>

Displays the version information for B<tachk> and the DNSSEC-Tools package.

=item B<-help>

Display a usage message.

=back

=head1 AUTHOR

Wesley Griffin

(Current contact for B<tachk> is Wayne Morrison,
tewok@tislabs.com.)

=head1 SEE ALSO

B<trustman(8)>

B<named.conf(5)>

=cut