File: cleankrf

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 (587 lines) | stat: -rwxr-xr-x 12,777 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
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
#!/usr/bin/perl
#
# Copyright 2005-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# cleankrf
#
#	This script cleans unused key keyrecs from a keyrec.
#

use strict;

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

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

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

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

#
# Data required for command line options.
#
my $count   = 0;			# Error-count flag.
my $list    = 0;			# List-only flag.
my $quiet   = 0;			# Quiet flag.
my $remove  = 0;			# Remove key file flag.
my $verbose = 0;			# Verbose flag.
my %options = ();			# Filled option array.
my @opts =
(
	"count",			# Give final error count.
	"list",				# Only list orphaned keys, don't delete.
	"quiet",			# Don't give any output.
	"rm",				# Remove key file.
	"verbose",			# Give lotsa output.
	"Version",			# Show version number.
	"help",				# Give a usage message and exit.
);

my $orphsets	= 0;			# Total count of orphaned sets.
my $orphans	= 0;			# Total count of orphaned keys.
my $obsoletes	= 0;			# Total count of obsolete keys.


my @krnames;				# List of keyrecs in the file.

my %zones = ();				# Names of zone keyrecs.
my %sets = ();				# Names of set keyrecs.
my %keys = ();				# Names of key keyrecs.

main();
exit($orphans);

#-----------------------------------------------------------------------------
# Routine:	main()
#
# Purpose:	This is the top-level processing routine for the command.
#
sub main
{
	my $argc = @ARGV;		# Number of command line arguments.

	erraction(ERR_EXIT);

	#
	# Check our options.
	#
	optsandargs();

	#
	# Search each specified keyrec file individually for orphaned
	# key keyrecs.
	#
	while($argc > 0)
	{
		#
		# Read the keyrec file.
		#
		getkeyrecs($ARGV[0]);

		#
		# Check for orphaned signing sets.
		#
		setchecks();

		#
		# Check for orphaned and obsolete keys.
		#
		keychecks();

		#
		# Move on to the next keyrec file.
		#
		shift @ARGV;
		$argc = @ARGV;
	}

	#
	# Save the modified keyrec file -- iff we're not just listing.
	#
	keyrec_write() if(!$list);

	#
	# If -count was given, give the counts of orphaned and obsolete keys.
	#
	if($count)
	{
		#
		# Check for orphaned sets.
		#
		if($orphsets == 1) { print("1 orphaned set\n"); }
		else		   { print("$orphsets orphaned sets\n"); }

		#
		# Check for orphaned keys.
		#
		if($orphans == 1) { print("1 orphaned key\n"); }
		else		  { print("$orphans orphaned keys\n"); }

		#
		# Check for obsolete keys.
		#
		if($obsoletes == 1) { print("1 obsolete keys\n"); }
		else		    { print("$obsoletes obsolete keys\n"); }
	}
}

#-----------------------------------------------------------------------------
# Routine:	optsandargs()
#
# Purpose:	This routine processes the command's options and arguments.
#
sub optsandargs
{
	my $argc = @ARGV;		# Number of command line arguments.

	GetOptions(\%options,@opts) || usage();
	$count	 = $options{'count'};
	$list	 = $options{'list'};
	$quiet	 = $options{'quiet'};
	$remove	 = $options{'rm'};
	$verbose = $options{'verbose'};

	usage()   if(defined($options{'help'}));
	version() if(defined($options{'Version'}));

	#
	# Can't have verbosity and quietude.
	#
	$verbose = 0 if($quiet);

	#
	# Ensure we were given a keyrec file to check.
	#
	usage() if($argc == 0);
}

#-----------------------------------------------------------------------------
# Routine:	getkeyrecs()
#
# Purpose:	This routine reads a keyrec file and puts each keyrec into
#		either a zone hash or a key hash.
#
sub getkeyrecs
{
	my $krfile = shift;			# Keyrec file.

	#
	# Read the keyrec file and get a list of the keyrec names.
	#
	keyrec_read($krfile);
	@krnames = keyrec_names();

	#
	# Go through each keyrec and put it in the appropriate hash table.
	#
	foreach my $krn (sort(@krnames))
	{
		my $kr;				# Reference to keyrec.
		my %keyrec;			# Keyrec.
		my $type;			# Keyrec's type.
		my $styp;			# Keyrec's set type.

		$kr = keyrec_fullrec($krn);
		%keyrec = %$kr;

		$type = $keyrec{'keyrec_type'};
		$styp = $keyrec{'set_type'};

		if($type eq 'zone')
		{
			$zones{$krn} = $kr;
		}
		elsif(($type eq 'set') ||
		      (($type eq 'kskobs') && ($styp eq 'kskobs')))
		{
			$sets{$krn} = $kr;
		}
		else
		{
			$keys{$krn} = $kr;
		}
	}
}

#-----------------------------------------------------------------------------
# Routine:      setchecks()
#
# Purpose:      This routine searches the list of signing sets in a keyrec
#               file for orphaned sets.  If found, the orphans are deleted,
#		unless the -list option was specified.
#
sub setchecks
{
	#
	# If there are no signing sets, we have nothing here to do.
	#
	if(length(%sets) == 0)
	{
		vprint("no signing sets defined\n");
		return;
	}

	#
	# Check each key to see if it is an orphan.  If so, we'll delete it,
	# unless the user just wants a list of orphans.
	#
	foreach my $setname (sort(keys(%sets)))
	{
		my $zone = $sets{$setname}{'zonename'};		# Set's zone.

		#
		# Handle two error conditions:
		#	- the set's zone doesn't exist
		#	- the set isn't a current set in its zone
		#
		if(!defined($zones{$zone}))
		{
			qprint("warning:  set $setname references $zone, but $zone does not exist\n");
			$orphsets++;
		}
		elsif(($zones{$zone}{'zskcur'} ne $setname)	&&
		      ($zones{$zone}{'zskpub'} ne $setname)	&&
		      ($zones{$zone}{'zsknew'} ne $setname)	&&
		      ($zones{$zone}{'zsknew'} ne $setname)	&&
		      ($zones{$zone}{'kskcur'} ne $setname)	&&
		      ($zones{$zone}{'kskpub'} ne $setname)
		     )
		{
			qprint("obsolete set:  $setname\n");
			$orphsets++;

			#
			# Resetting the keyrec_type to ensure that obsolete
			# KSK sets will actually be deleted.
			#
			keyrec_setval('set',$setname,'keyrec_type','set');
		}
		else
		{
			vprint("referenced set:  $setname  ($zone)\n");
			next;
		}

		#
		# Go to the next set if we're just listing or if this
		# one is okay.
		#
		next if($list);

		#
		# Delete the set.
		#
		keyrec_del($setname);
		delete $sets{$setname};
	}
}

#-----------------------------------------------------------------------------
# Routine:	keychecks()
#
# Purpose:	This routine searches the list of keys in a keyrec file for
#		orphaned keys.  If found, the orphans are deleted, unless
#		the -list option was specified.
#
sub keychecks
{
	#
	# If there are no keys, we have nothing here to do.
	#
	if(length(%keys) == 0)
	{
		vprint("no keys defined\n");
		return;
	}

	#
	# Check each key to see if it is obsolete.  If so, we'll delete it,
	# unless the user just wants a list of obsolete keys.
	#
	# This should find anything, as obsolete keys should be orphans.
	# However, we'll do this check just to be sure.
	#
	foreach my $keyname (sort(keys(%keys)))
	{
		my $kr;				# Keyrec reference.
		my %keyrec;			# Keyrec hash.

		$kr = $keys{$keyname};
		%keyrec = %$kr;

		if(($keyrec{'keyrec_type'} eq "zskobs")	||
		   ($keyrec{'keyrec_type'} eq "kskobs")	||
		   ($keyrec{'keyrec_type'} eq "kskrev"))
		{
			qprint("obsolete key:  $keyname\n");
			$obsoletes++;

			next if($list);

			rmkey($keyname) if($remove);
			keyrec_del($keyname);
			delete $keys{$keyname};
		}
	}

	#
	# Check each key to see if it is an orphan.  If so, we'll delete it,
	# unless the user just wants a list of orphans.
	#
	foreach my $keyname (sort(keys(%keys)))
	{
		if(orphankey($keyname))
		{
			next if($list);

			rmkey($keyname) if($remove);
			keyrec_del($keyname);
			delete $keys{$keyname};
		}
	}
}

#-----------------------------------------------------------------------------
# Routine:	orphankey()
#
# Purpose:	This routine checks if a given key is an orphan or if it's
#		referenced by a signing set.
#		The orphan count is incremented when an orphan is found.
#
# Return Values:
#		0 - referenced key
#		1 - orphaned key
#
sub orphankey
{
	my $keyname = shift;			# Keyrec's name.
	my $kr;					# Keyrec reference.
	my %keyrec;				# Key's keyrec.
	my @keysets = ();			# Set list.
	my $found = 0;				# Found flag.

	#
	# Get the key's keyrec and save the its zone name.
	#
	$kr = $keys{$keyname};
	%keyrec = %$kr;

	#
	# Check the zones to see if one references this key.
	#
	foreach my $setname (sort(keys(%sets)))
	{
		if(keyrec_signset_haskey($setname,$keyname))
		{
			push @keysets, $setname;
			$found = 1;
		}
	}

	#
	# Give an appropriate message.  If the key was found, we'll
	# report all the sets it belongs to.
	#
	if($found)
	{
		my $setlist;			# All of key's sets.

		$setlist = join ' ', @keysets;
		vprint("referenced key:  $keyname $setlist\n");
		return(0);
	}

	#
	# This key isn't referenced by any of our zones, so it's an orphan.
	# Bump our counter and give an error message.
	#
	qprint("orphaned key:  $keyname\n");
	$orphans++;
	return(1);
}

#-----------------------------------------------------------------------------
# Routine:	rmkey()
#
# Purpose:	This routine deletes the .key and .private file associated
#		with the key specified by the caller.
#
sub rmkey
{
	my $keyname = shift;			# Keyrec's name.
	my $keyrec;				# Key's keyrec.
	my $fname;				# Name of key to be deleted.

	#
	# Get the key's keyrec and save its pathname.
	#
	$keyrec = $keys{$keyname};
	$fname	= $keyrec->{'keypath'};
	return if($fname eq "");

	#
	# Delete the public half of the key.
	#
	print "\tunlinking $fname\n" if($verbose);
	unlink($fname);

	#
	# Delete the private half of the key.
	#
	$fname =~ s/key$/private/;
	print "\tunlinking $fname\n" if($verbose);
	unlink($fname);
}

#-----------------------------------------------------------------------------
# Routine:	qprint()
#
# Purpose:	Print the given output iff -quiet wasn't specified.
#
sub qprint
{
	my $line = shift;

	print $line if(!$quiet);
}

#-----------------------------------------------------------------------------
# Routine:	vprint()
#
# Purpose:	Print the given output iff -verbose was specified.
#
sub vprint
{
	my $line = shift;

	print $line if($verbose);
}

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

	exit(0);
}

#-----------------------------------------------------------------------------
# Routine:	usage()
#
sub usage
{
	print STDERR "usage:  cleankrf [options] <keyrec files>\n";
	print STDERR "\toptions:\n";
	print STDERR "\t\t-count    -  give count of orphan keys\n";
	print STDERR "\t\t-list     -  only list orphaned keys\n";
	print STDERR "\t\t-rm       -  delete orphaned keys\n";
	print STDERR "\t\t-quiet    -  don't give any output\n";
	print STDERR "\t\t-verbose  -  give much output\n";
	print STDERR "\t\t-Version  -  show the version number\n";
	print STDERR "\t\t-help     -  give a usage message and exit\n";

	exit(0);
}

1;

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

=pod

=head1 NAME

cleankrf - Clean a DNSSEC-Tools I<keyrec> files of old data

=head1 SYNOPSIS

  cleankrf [options] <keyrec-files>

=head1 DESCRIPTION

B<cleankrf> cleans old data out of a set of DNSSEC-Tools I<keyrec> files.
The old data are obsolete signing sets, orphaned keys, and obsolete keys.

Obsolete signing sets are set I<keyrec>s unreferenced by a zone I<keyrec>.
Revoked signing sets are considered obsolete by B<cleankrf>.

Orphaned keys are KSK and ZSK key I<keyrec>s unreferenced by a set I<keyrec>.

Obsolete keys are key I<keyrec>s with a I<keyrec_type> of B<kskobs> or
B<zskobs>.

B<cleankrf>'s exit code is the count of orphaned and obsolete I<keyrec>s
found.

=head1 OPTIONS

=over 4

=item B<-count>

Display a final count of old I<keyrec>s found in the I<keyrec> files.  This
option allows the count to be displayed even if the B<-quiet> option is given.

=item B<-list>

The key I<keyrec>s are checked for old I<keyrec>s, but they are not removed
from the I<keyrec> file.  The names of the old I<keyrec>s are displayed.

=item B<-rm>

Delete the key files, both B<.key> and B<.private>, from orphaned and
expired I<keyrec>s.

=item B<-quiet>

Display no output.

=item B<-verbose>

Display output about referenced keys and unreferenced keys.

=item B<-Version>

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

=item B<-help>

Display a usage message.

=back

=head1 COPYRIGHT

Copyright 2004-2012 SPARTA, Inc.  All rights reserved.
See the COPYING file included with the DNSSEC-Tools package for details.

=head1 AUTHOR

Wayne Morrison, tewok@tislabs.com

=head1 SEE ALSO

B<fixkrf(8)>,
B<lskrf(8)>,
B<zonesigner(8)>

B<Net::DNS::SEC::Tools::keyrec.pm(3)>

B<file-keyrec.pm(5)>

=cut