File: fixkrf

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 (327 lines) | stat: -rwxr-xr-x 6,849 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
#!/usr/bin/perl
#
# Copyright 2004-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# fixkrf
#
#	This script fixes keyrec files whose encryption key files have
#	been moved.
#

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   = "fixkrf";
my $VERS   = "$NAME version: 1.13.0";
my $DTVERS = "DNSSEC-Tools Version: 1.13";

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

#
# Data required for command line options.
#
my $list    = 0;			# List-only flag.
my $verbose = 0;			# Verbose flag.
my %options = ();			# Filled option array.
my @OPTS =
(
	"list",				# List, but don't do anything.
	"verbose",			# Give lotsa output.
	"help",				# Give a usage message and exit.
	"Version",			# Display the version number.
);

my $krfile = "";			# Keyrec file to check.
my @dirs = ();				# Directories that may hold key files.

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

my $errors = 0;				# Count of missing key files.

my $USELOC = 0;				# Show location in usage().

main();
exit(0);

#-----------------------------------------------------------------------------
# Routine:	main()
#
# Purpose:	This is the top-level processing routine for the command.
#
sub main
{
	my $kr;					# Reference to a keyrec.
	my $krn;				# Name of a keyrec.

	erraction(ERR_EXIT);

	#
	# Check our options.
	#
	optsandargs();

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

	#
	# Go through the keyrecs in the keyrec file and check each one for
	# valid keys.  We'll go through the file according to the list of
	# keyrec names.
	#
	foreach $krn (@krnames)
	{
		#
		# Get a reference to this name's keyrec.
		#
		$kr = keyrec_fullrec($krn);

		#
		# Don't do anything if this keyrec is a zone of a set.
		# If it's a key, we only have to check the keyrec itself.
		#
		if(($kr->{'keyrec_type'} eq "zone")	||
		   ($kr->{'keyrec_type'} eq "set"))
		{
			#
			# Don't do anything for zones or sets.
			#
		}
		else
		{
			print "key $krn\n" if($verbose);
			checkkey('key',$krn,'keypath',$kr->{'keypath'});
		}

		print "\n" if($verbose);
	}

	#
	# Save the keyrec file.
	#
	keyrec_write();

}

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

	#
	# Parse the options.
	#
	GetOptions(\%options,@OPTS) || usage();
	$list	 = $options{'list'};
	$verbose = $options{'verbose'};

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

	#
	# Ensure we were given a keyrec file to check and directories in
	# which to check.
	#
	usage(2) if($argc < 2);

	#
	# Save the arguments.
	#
	$krfile = shift(@ARGV);
	@dirs = @ARGV;
}

#-----------------------------------------------------------------------------
# Routine:	checkkey()
#
# Purpose:	
#
sub checkkey
{
	my $rectype = shift;			# Record type:  key or zone.
	my $krname  = shift;			# Keyrec name.
	my $kfield  = shift;			# Keyrec field name.
	my $keyfile = shift;			# Key file to look for.

	my $cnt;				# Found nodes.
	my $dir;				# Directory to search.
	my $node;				# Final node in path.
	my $path;				# Path = dir + keyname.
	my @found = ();				# Found directories.

	#
	# Ensure we were given a real file name.
	#
	return if($keyfile eq "");

	#
	# If the specified file exists, don't look further.
	#
	if(-e $keyfile)
	{
		print "$keyfile found\n" if($verbose);
		return;
	}

	#
	# Get the node in the given keyfile.
	#
	$keyfile =~ /^.*\/(.*)$/;
	$node = $1;

	#
	# Look for the key file in each of the user's named directories.
	#
	foreach $dir (@dirs)
	{
		$path = "$dir/$node";	
		push(@found,$dir) if(-e $path);
	}

	#
	# If we didn't find the key, give an error and return.
	#
	$cnt = @found;
	if($cnt == 0)
	{
		print STDERR "$keyfile does not exist\n";
		$errors++;
		return;
	}

	#
	# If we found a single instance, we'll adjust the keyrec to
	# reference the found file.
	#
	if($cnt == 1)
	{
		$path = "$found[0]/$node";
		keyrec_setval($rectype,$krname,$kfield,$path) if(!$list);
		print STDERR "$keyfile found, moving to $path\n" if($verbose);
		return;
	}

	#
	# We've found a file with this name in several places.  Give
	# an error and don't do anything about it.
	#
	print STDERR "\"$keyfile\" exists in multiple directories (@found);\nnot doing anything...\n";
	$errors++;

}

#----------------------------------------------------------------------
# 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
{
	my $loc = shift;				# Call location.

	print STDERR "usage:  fixkrf [options] <keyrec file> <dir 1> ... <dir N>\n";
	print STDERR "\toptions:\n";
	print STDERR "\t\t-list     -  give output, but don't fix the keyrec\n";
	print STDERR "\t\t-verbose  -  give lots of output\n";
	print STDERR "\t\t-Version  -  display version number\n";
	print STDERR "\t\t-help     -  give a usage message and exit\n";

	print "\ncalled from $loc\n" if($USELOC);
	exit(0);
}

1;

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

=pod

=head1 NAME

fixkrf - Fixes DNSSEC-Tools I<keyrec> files whose encryption key files have
been moved

=head1 SYNOPSIS

  fixkrf [options] <keyrec-file> <dir 1> ... <dir N>

=head1 DESCRIPTION

B<fixkrf> checks a specified I<keyrec> file to ensure that the referenced
encryption key files exist where listed.  If a key is not where the I<keyrec>
specifies it should be, then B<fixkrf> will search the given directories for
those keys and adjust the I<keyrec> to match reality.  If a key of a
particular filename is found in multiple places, a warning will be printed
and the I<keyrec> file will not be changed for that key.

=head1 OPTIONS

=over 4

=item B<-list>

Display output about missing keys, but don't fix the I<keyrec> file.

=item B<-verbose>

Display output about found keys as well as missing keys.

=item B<-Version>
    
Display version information for B<fixkrf> and DNSSEC-Tools.

=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<cleankrf(8)>,
B<genkrf(8)>,
B<lskrf(8)>,
B<zonesigner(8)>

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

B<file-keyrec.pm(5)>

=cut