File: realmset

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 (687 lines) | stat: -rwxr-xr-x 16,270 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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/perl
#
# Copyright 2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# realmset
#
#	This script sets some values in a realm file.
#

use strict;

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

use Net::DNS::SEC::Tools::realm;
use Net::DNS::SEC::Tools::tooloptions;

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

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


my %options = ();			# Filled option array.
my @opts =
(
	'name=s',			# Realmrec to edit.
	'administrator=s',		# New administrator value.
	'args=s',			# New arguments value.
	'configdir=s',			# New config directory value.
	'display!',			# New display value.
	'manager=s',			# New realm manager value.
	'newname=s',			# New name for rollrec.
	'realmdir=s',			# New realm directory value.
	'rollrec=s',			# New rollrec value.
	'state=s',			# New realm state value.
	'statedir=s',			# New state directory value.
	'user=s',			# New user value.
	'del-administrator',		# Delete the administrator line.
	'del-args',			# Delete the arguments line.
	'del-display',			# Delete the display line.
	'del-manager',			# Delete the manager line.
	'del-user',			# Delete the user line.

	'nocheck',			# Don't run realmchk after edits.
	'verbose',			# Give lotsa output.
	'Version',			# Display the version number.
	'help',				# Give a usage message and exit.
);

#
# Data required for command line options.
#
my $name	= '';
my $admin	= '';
my $args	= '';
my $confdir	= '';
my $display	= 0;
my $manager	= '';
my $newname	= '';
my $nodisplay	= 0;
my $realmdir	= '';
my $rollrec	= '';
my $state	= '';
my $statedir	= '';
my $user	= '';
my $deladmin	= 0;
my $delargs	= 0;
my $deldisp	= 0;
my $delmanager	= 0;
my $deluser	= 0;

#
# Flag variables for options.
#
my $verbose = 0;			# Verbose flag.
my $check   = 0;			# Check-realm flag.
my $doall   = 0;			# Edit all realm entries flag.


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

my $rmf;				# Realm file we're editing.

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

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

#-----------------------------------------------------------------------------
# Routine:	main()
#
# Purpose:	This routine controls everything.
#
sub main()
{
	#
	# Check our options.
	#
	doopts();

	#
	# Edit the realm file.
	#
	editrmf();

	#
	# Maybe check the realm file for validity.
	#
	if($check)
	{
		my $cmd = $verbose ? "realmchk -v" : "realmchk";

		system("$cmd $rmf");
	}

	return(0);
}

#-----------------------------------------------------------------------------
# Routine:	doopts()
#
# Purpose:	This routine gets the options from the command line and does
#		a bit of validity checking.
#
sub doopts
{
	my $errs = 0;					# Error count.

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

	#
	# Ensure we were given a realm file to check.
	#
	usage() if(@ARGV == 0);

	#
	# Show the version number or help info if requested.
	#
	version() if(defined($options{'Version'}));
	usage()   if(defined($options{'help'}));

	#
	# Set some flags based on the command line.
	#
	$name	    = $options{'name'};
	$admin	    = $options{'administrator'};
	$args	    = $options{'args'};
	$confdir    = $options{'configdir'};
	$display    = $options{'display'};
	$manager    = $options{'manager'};
	$newname    = $options{'newname'};
	$nodisplay  = $options{'nodisplay'};
	$realmdir   = $options{'realmdir'};
	$rollrec    = $options{'rollrec'};
	$state	    = $options{'state'};
	$statedir   = $options{'statedir'};
	$user	    = $options{'user'};
	$deladmin   = $options{'del-administrator'};
	$delargs    = $options{'del-args'};
	$deldisp    = $options{'del-display'};
	$delmanager = $options{'del-manager'};
	$deluser    = $options{'del-user'};

	$check	   = $options{'nocheck'} ? 0 : 1;
	$verbose   = $options{'verbose'};

	#
	# Ensure we're not being asked to do the impossible.
	# (The stupid, actually.)
	#
	if(defined($state) && ($state ne 'active') && ($state ne 'inactive'))
	{
		print STDERR "state must be either \"active" or \"inactive\"\n";
		$errs++;
	}
	if(defined($display) && (defined($nodisplay)))
	{
		print STDERR "-display and -nodisplay are mutually exclusive\n";
		$errs++;
	}
	if(defined($admin) && (defined($deladmin)))
	{
		print STDERR "-administrator and -del-administrator are mutually exclusive\n";
		$errs++;
	}
	if(defined($args) && (defined($delargs)))
	{
		print STDERR "-args and -del-args are mutually exclusive\n";
		$errs++;
	}
	if(defined($display) && (defined($deldisp)))
	{
		print STDERR "-display and -del-display are mutually exclusive\n";
		$errs++;
	}
	if(defined($nodisplay) && (defined($deldisp)))
	{
		print STDERR "-nodisplay and -del-display are mutually exclusive\n";
		$errs++;
	}
	if(defined($manager) && (defined($delmanager)))
	{
		print STDERR "-manager and -del-manager are mutually exclusive\n";
		$errs++;
	}
	if(defined($user) && (defined($deluser)))
	{
		print STDERR "-user and -del-user are mutually exclusive\n";
		$errs++;
	}

	#
	# Convert the realm-manager arguments into their real form.
	#
	if($args ne '')
	{
		my @args = split / /, $args;

		for(my $ind=0; $ind < @args; $ind++)
		{
			$args[$ind] =~ s/^=/-/;
			$args[$ind] =~ s/=/ /;
		}
		$args = join ' ', @args;
	}

	#
	# If a realm name wasn't specified, we'll apply the fix to every
	# realm in the file.
	#
	$doall = 1 if(! $name);

	#
	# Ensure that -rename will be applied to a specific realm.
	#
	if($newname && $doall)
	{
		print STDERR "-rename may only be used with a single realm\n";
		$errs++;
	}

	#
	# Delete the non-command options and ensure that we were given
	# something to do.
	#
	delete $options{'name'};
	delete $options{'nocheck'};
	delete $options{'verbose'};
	delete $options{'Version'};
	delete $options{'help'};
	if(keys(%options) == 0)
	{
		print STDERR "you must specify something to be changed\n";
		$errs++;
	}

	#
	# Exit if there were any errors.
	#
	exit(1) if($errs);

	#
	# Save the name of the realm file.
	#
	$rmf = $ARGV[0];
}

#-----------------------------------------------------------------------------
# Routine:	editrmf()
#
# Purpose:	This routine reads a realm file and modifies the records
#		according to the command-line options.  After making its
#		changes, the realm file is rewritten with the new contents.
#
sub editrmf
{
	#
	# Lock and load the realm file.
	#
	realm_lock();
	if(realm_read($rmf) < 0)
	{
		realm_unlock();
		print STDERR "unable to read realm file \"$rmf\"\n";
		exit(-1);
	}

	#
	# Change the realm name, if so requested.
	#
	if($newname)
	{
		my $ret;				# Rename return code.

		$ret = realm_rename($name,$newname);
		if($ret == 0)
		{
			vprint("name:  realm name changed to \"$newname\"\n");
		}
		elsif($ret == -3)
		{
			print STDERR "unable to rename \"$name\"; $name is not a valid realm name\n";
		}
		elsif($ret == -4)
		{
			print STDERR "unable to rename \"$name\"; $newname is already a realm name\n";
		}
		elsif($ret == -5)
		{
			print STDERR "unable to rename \"$name\"\n";
		}
	}

	#
	# Go through the realms and apply the needed changes.
	#
	foreach my $rname (realm_names())
	{
		#
		# Go to the next record if we aren't doing everything or if
		# this isn't the specified record.
		#
		next if(!$doall && ($rname ne $name));

		#
		# Set the realm fields as requested by the user.
		#
		changer($admin,$rname,'administrator',$admin);
		changer($args,$rname,'args',$args);
		changer($confdir,$rname,'configdir',$confdir);
		changer($manager,$rname,'manager',$manager);
		changer($realmdir,$rname,'realmdir',$realmdir);
		changer($rollrec,$rname,'rollrec',$rollrec);
		changer($state,$rname,'state',$state);
		changer($statedir,$rname,'statedir',$statedir);
		changer($user,$rname,'user',$user);
		changer($display,$rname,'display',1);
		changer($nodisplay,$rname,'display',0);

		#
		# Delete some fields.
		#
		deleter($deladmin,$rname,'administrator');
		deleter($delargs,$rname,'args');
		deleter($deldisp,$rname,'display');
		deleter($delmanager,$rname,'manager');
		deleter($deluser,$rname,'user');
	}

	#
	# Close, write, and unlock the realm file.
	#
	realm_close();
	realm_unlock();
}

#----------------------------------------------------------------------
# Routine:	changer()
#
# Purpose:	Change a realm field value and maybe give verbose messages.
#
sub changer
{
	my $flag  = shift;				# Flag value.
	my $rname = shift;				# Realm name.
	my $field = shift;				# Field to change.
	my $val	  = shift;				# Field's new value.

	#
	# Do nothing if this field shouldn't be changed.
	#
	return if(! $flag);

	#
	# If the verbose flag was given, we'll show the old value and
	# the new value.
	#
	if($verbose)
	{
		my $oldval;				# Old value.

		$oldval = realm_recval($rname,$field);
		print "$rname:  changing $field \"$oldval\" to \"$val\"\n";
	}

	#
	# Change the realm field's value.
	#
	realm_setval($rname,$field,$val);
}

#----------------------------------------------------------------------
# Routine:	deleter()
#
# Purpose:	Delete a realm field and maybe give verbose messages.
#
sub deleter
{
	my $flag  = shift;				# Flag value.
	my $rname = shift;				# Realm name.
	my $field = shift;				# Field to delete.

	my $val;					# Value of field.

	#
	# Do nothing if this field shouldn't be deleted.
	#
	return if(!$flag);

	#
	# Ensure the field exists for the realm.
	#
	$val = realm_recval($rname,$field);
	if(!defined($val))
	{
		print "$rname does not have a $field field\n" if(! $doall);
		return;
	}

	#
	# Maybe show the current value.
	#
	vprint("$rname:  deleting $field \"$val\"\n");

	#
	# Delete the realm field.
	#
	realm_delfield($rname,$field);
}

#----------------------------------------------------------------------
# Routine:	vprint()
#
# Purpose:	Print the given string if the verbose flag is set.
#
sub vprint
{
	my $str = shift;

	print $str 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:  realmset [options] <realm-file>\n";
	print STDERR "	options:\n";

	print STDERR "		-name realm-name\n";
	print STDERR "		-newname new-realm-name\n";
	print STDERR "		-administrator admin-email\n";
	print STDERR "		-args rollerd-arguments\n";
	print STDERR "		-configdir configuration-directory\n";
	print STDERR "		-display\n";
	print STDERR "		-manager realm-manager\n";
	print STDERR "		-nodisplay\n";
	print STDERR "		-realmdir realm-directory\n";
	print STDERR "		-rollrec rollrec-file\n";
	print STDERR "		-state realm-state\n";
	print STDERR "		-statedir state-directory\n";
	print STDERR "		-user username\n";

	print STDERR "\n";

	print STDERR "		-del-administrator\n";
	print STDERR "		-del-args\n";
	print STDERR "		-del-display\n";
	print STDERR "		-del-manager\n";
	print STDERR "		-del-user\n";

	print STDERR "\n";

	print STDERR "		-nocheck\n";
	print STDERR "		-verbose\n";
	print STDERR "		-Version\n";
	print STDERR "		-help\n";

	exit(0);
}

1;

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

=pod

=head1 NAME

realmset - Modifies entries in a DNSSEC-Tools I<realm> file

=head1 SYNOPSIS

  realmset [options] realm-file

=head1 DESCRIPTION

B<realmset> modifies fields in the I<realm> file specified by I<realm-file>.
Multiple options may be combined in a single B<realmset> execution.
B<realmset> operates quietly unless it is given the I<-verbose> option.

All records in the specified I<realm> file will be modified, unless the
B<-name> option is given.  In that case, only the named zone will be modified.

=head1 OPTIONS

=over 4

=item B<-administrator addr>

The zone administrator's email address is set to I<addr>.

=item B<-args arglist>

Provides additional arguments for the realm manager.  These arguments will
override the arguments in the DNSSEC-Tools defaults file, the DNSSEC-Tools
configuration file, and the realms' I<rollrec> files.  The manager's
argument list is given in I<arglist>.  If more than one argument is given,
the set of arguments should be enclosed in quotes.

Given the B<realmset> argument processing, the new arguments cannot be
specified as they would from a command line.  Instead, the arguments should
be given in the following manner.  The leading dash should be replaced with
an equals sign.  If the option takes an argument, the space that would
separate the option from the option's argument should also be replaced by
an equals sign.  B<realmset> translates these arguments to the appropriate
format for the realm manager.  These examples should clarify the argument
modifications:

    normal rollerd option		-args options
    ------------------------		---------------
	-display			   =display
	-logfile /dt/log.file		   =zskcount=/dt/log.file

The following are valid uses of I<-args>:

    # realmset -args =display example.realm
    # realmset -args "=loglevel=phase =sleep=3600" example.realm

The B<-args> and B<-del-args> options are mutually exclusive.

=item B<-configdir configdir>

The directory to hold the realm's configuration files is set to I<confdir>.

=item B<-del-administrator>

The I<administrator> line is deleted from the selected I<realm> records.
The B<-administrator> and B<-del-administrator> options are mutually exclusive.

=item B<-del-args>

The I<args> line is deleted from the selected I<realm> records.
The B<-args> and B<-del-args> options are mutually exclusive.

=item B<-del-display>

The I<display> line is deleted from the selected I<realm> records.
The B<-display>, B<-nodisplay>, and B<-del-display> options are mutually
exclusive.

=item B<-del-manager>

The I<manager> line is deleted from the selected I<realm> records.
The B<-manager> and B<-del-manager> options are mutually exclusive.

=item B<-del-user>

The I<user> line is deleted from the selected I<realm> records.
The B<-user> and B<-del-user> options are mutually exclusive.

=item B<-display>

Turn on the GUI display of the selected I<realm>s.
The B<-display>, B<-nodisplay>, and B<-del-display> options are mutually
exclusive.

=item B<-manager manager-program>

The manager program for the selected realms is set to I<manager-program>.

=item B<-name realmname>

The I<realm> whose name matches I<realmname> is selected as the only realm
that will be modified.  If this name is not given, then all I<realm> records
will be modified.

=item B<-newname new-name>

The I<realm>'s name is changed to B<new-name>.
The new name cannot be the name of an existing I<realm> in the file.
This option must be used in conjunction with the B<-name> option.

=item B<-nocheck>

If this option is given, the B<realmchk> command will B<not> be run on
the modified I<realm> file.

=item B<-nodisplay>

Turn off the GUI display of the selected I<realm>s.
The B<-display>, B<-nodisplay>, and B<-del-display> options are mutually
exclusive.

=item B<-realmdir realmdir>

The directory to hold the realm's files is set to I<realmdir>.

=item B<-rollrec rollrec-file>

The I<rollrec> file in the selected I<realm> records is modified to be
I<rollrec-file>.

=item B<-state newstate>

The realm's state is set to I<newstate>.  This must be either "active" or
"inactive".

=item B<-statedir statedir>

The directory to hold the realm's state files is set to I<statedir>.

=item B<-user username>

The user name in the selected I<realm> records is modified to be
I<username>.

=item B<-verbose>

Display information about every modification made to the I<realm> file.

=item B<-Version>

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

=item B<-help>

Display a usage message.

=back

=head1 COPYRIGHT

Copyright 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<dtrealms(8)>,
B<lsrealm(8)>,
B<realmchk(8)>,
B<realminit(8)>

B<Net::DNS::SEC::Tools::realmmgr.pm(3)>,
B<Net::DNS::SEC::Tools::realm.pm(3)>

B<file-realm(5)>

=cut