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 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
|
#!/usr/bin/perl
#
# Copyright 2012 SPARTA, Inc. All rights reserved. See the COPYING
# file distributed with this software for details.
#
#
# lsrealm
#
# This script lists the realms 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 = "lsrealm";
my $VERS = "$NAME version: 1.13.0";
my $DTVERS = "DNSSEC-Tools Version: 1.13";
#######################################################################
#
# Data required for command line options.
#
my %options = (); # Filled option array.
my @opts =
(
# Records to select:
'active', # Active realms.
'inactive', # Inactive realms.
# Data to display:
'state', # Status of realm.
'realmdir', # Realm directory.
'configdir', # Configuration directory.
'rollrec', # Realm's rollrec file.
'admin', # Realm administrator.
'display', # Display flag.
'manager', # Rollover manager.
'args', # User-specified rollerd arguments.
'user', # User to execute realm.
# Output format:
'count', # Only give count of matching realms.
'fullpaths', # Show full pathnames.
'headers', # Give a column header.
'terse', # Give terse output.
'long', # Give long output.
'Version', # Display the version number.
'help', # Give a usage message and exit.
);
#
# Flag values for the various options. Variable/option connection should
# be obvious.
#
my $activeflag;
my $inactiveflag;
my $stateflag;
my $nameflag;
my $realmdirflag;
my $configdirflag;
my $rollrecflag;
my $adminflag;
my $dispflag;
my $managerflag;
my $argsflag;
my $userflag;
my $cntflag;
my $fullpaths = 0; # Display full pathnames.
my $headerflag;
my $terse;
my $long;
my $version = 0; # Display the version number.
my $count = 0; # Record-match count.
#######################################################################
my $HEADER_RRN = "<<<header>>>"; # Name for column header "realm".
my @rrnames; # List of realms in the files.
my %realms = (); # Realms from the realm files.
my %lengths = (); # Maximum lengths of realm field.
my $prevkey; # Previous realm key added to output.
my $ret; # Return code from main().
$ret = main();
exit($ret);
#-----------------------------------------------------------------------------
# Routine: main()
#
# Purpose: Main controller routine for lsrealm.
#
sub main()
{
my $argc = @ARGV; # Number of command line arguments.
my $errors = 0; # Total error count.
my $cnt = 0; # Total realms read.
#
# Check our options.
#
doopts($argc);
#
# Read the realm files.
#
while($argc > 0)
{
$cnt += getrealms($ARGV[0]);
shift @ARGV;
$argc = @ARGV;
}
#
# Exit if we didn't find any realms at all.
#
return(1) if($cnt == 0);
#
# Cook up the output: build a header line and calculate the maximum
# length of each field.
#
makeheader();
maxlens();
#
# Display the requested data.
#
showrealms();
#
# 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.
#
$activeflag = $options{'active'} || 0;
$inactiveflag = $options{'inactive'} || 0;
$cntflag = $options{'count'} || 0;
$headerflag = $options{'headers'} || 0;
$terse = $options{'terse'} || 0;
$long = $options{'long'} || 0;
#
# Show the version number if requested
#
version() if(defined($options{'Version'}));
#
# Give a usage flag if asked.
#
usage() if(defined($options{'help'}));
#
# Check for conflicting options.
#
if($activeflag && $inactiveflag)
{
print STDERR "please select only one of -active and -inactive\n";
exit(0);
}
#
# Ensure we were given a realm file to check.
#
$argc = @ARGV;
usage() if($argc == 0);
#############################################################
#
# WARNING: Code order beyond this point is critical. Do *NOT* modify
# anything in the rest of this routine if you are an idiot.
#
#
# Make sure conflicting options weren't given.
#
usage() if($long && $terse);
#
# Set the output flags for -long output.
#
if($long)
{
$stateflag = 1;
$nameflag = 1;
$realmdirflag = 1;
$configdirflag = 1;
$rollrecflag = 1;
$managerflag = 1;
$adminflag = 1;
$argsflag = 1;
$dispflag = 1;
$userflag = 1;
}
elsif($terse)
{
#
# Set the output flags for -terse output.
#
$stateflag = 0;
$nameflag = 1;
$realmdirflag = 0;
$configdirflag = 0;
$rollrecflag = 0;
$managerflag = 0;
$adminflag = 0;
$argsflag = 0;
$dispflag = 0;
$userflag = 0;
}
else
{
#
# Set the default flag values.
#
$stateflag = 1;
$nameflag = 1;
$realmdirflag = 0;
$configdirflag = 0;
$rollrecflag = 0;
$adminflag = 0;
$managerflag = 0;
$argsflag = 0;
$dispflag = 0;
$userflag = 0;
}
#
# Now fold in the option values...
#
$stateflag = 1 if($options{'state'});
$realmdirflag = 1 if($options{'realmdir'});
$configdirflag = 1 if($options{'configdir'});
$rollrecflag = 1 if($options{'rollrec'});
$managerflag = 1 if($options{'manager'});
$adminflag = 1 if($options{'admin'});
$dispflag = 1 if($options{'display'});
$argsflag = 1 if($options{'args'});
$userflag = 1 if($options{'user'});
$fullpaths = 1 if($options{'fullpaths'});
}
#-----------------------------------------------------------------------------
# Routine: getrealms()
#
# Purpose: This routine reads the specified realm file and puts each
# realm into the realmrec hash table.
# A little data-massage also takes place:
# - default admins are added to admin-less records
# - default managers are added to managers-less records
#
sub getrealms
{
my $rrfile = shift; # Realm file.
my $realmcnt = 0; # Count of realms found.
realm_lock();
realm_read($rrfile);
realm_unlock();
#
# Load the realms from the realm file.
#
@rrnames = realm_names();
foreach my $rn (sort(@rrnames))
{
my $rr; # Reference to realm.
my $state; # Realm's state.
#
# Get a reference to the realm.
#
$rr = realm_fullrec($rn);
#
# If we're not displaying this type of record, don't
# include it in the length calculate.
#
$state = $rr->{'state'};
if(($activeflag && ($state eq "inactive")) ||
($inactiveflag && ($state eq "active")))
{
next;
}
next if($rn eq '');
#
# Save the realm and bump our realm count.
#
$realms{$rn} = $rr;
$realmcnt++;
}
#
# Give an error message if no realms were found.
#
if($realmcnt == 0)
{
print STDERR "no realm entries found in $rrfile\n";
return(0);
}
#
# Transmogrify a few fields in the realm as needed.
#
foreach my $rn (sort(keys(%realms)))
{
#
# Go to the next if the name is empty.
#
next if($rn eq '');
#
# Add quotes to the realm.
#
$realms{$rn}{'realm_name'} = "$realms{$rn}{'realm_name'}";
#
# If the realm has no administrator, give it the default.
#
if(!defined($realms{$rn}{'administrator'}))
{
$realms{$rn}{'administrator'} = "(defadmin)";
}
#
# If the realm has no manager, give it the default.
#
if(!defined($realms{$rn}{'manager'}))
{
$realms{$rn}{'manager'} = "(defmgr)";
}
#
# Adjust pathnames as needed.
#
$realms{$rn}{'configdir'} = getpath($realms{$rn}{'configdir'});
$realms{$rn}{'realmdir'} = getpath($realms{$rn}{'realmdir'});
$realms{$rn}{'rollrec'} = getpath($realms{$rn}{'rollrec'});
#
# Add quotes to the rollover manager's arguments. If the
# realm has no args specified, give it a default message.
#
if(defined($realms{$rn}{'args'}))
{
$realms{$rn}{'args'} = "\"$realms{$rn}{'args'}\"";
}
else
{
$realms{$rn}{'args'} = "(none)";
}
}
#
# Return the number of realms we found in the realm file.
#
return($realmcnt);
}
#----------------------------------------------------------------------
# Routine: maxlens()
#
# Purpose: Calculate the maximum length of each realm field.
# After finding the longest length for each field, we'll
# add a little buffer space.
#
sub maxlens
{
#
# Loop through the realm list and give data on the desired realms.
#
foreach my $rrname (sort(keys(%realms)))
{
my $recref = $realms{$rrname};
my %realm = %$recref;
foreach my $key (sort(keys(%realm)))
{
if(length($realm{$key}) > $lengths{$key})
{
$lengths{$key} = length($realm{$key});
}
}
}
#
# Build a dummy entry for the phases option. This can be hardcoded
# since a -phases will only ever give output of the "M/N" form.
#
$lengths{'phases'} = 3 if(!defined($lengths{'phases'}));
#
# Add a little buffer space between fields.
#
foreach my $key (sort(keys(%lengths)))
{
$lengths{$key} += 4;
}
}
#----------------------------------------------------------------------
# Routine: makeheader()
#
# Purpose: Build a header line as a fake realm.
#
sub makeheader
{
my %rr; # New, fake realm.
return if(!$headerflag);
$rr{'realm_type'} = 'Type';
$rr{'administrator'} = 'Administrator';
$rr{'args'} = 'Manager Arguments';
$rr{'configdir'} = 'Configuration Directory';
$rr{'display'} = 'Display Flag';
$rr{'manager'} = 'Rollover Manager';
$rr{'realm_name'} = 'Name';
$rr{'realmdir'} = 'Realm Directory';
$rr{'rollrec'} = 'Rollrec';
$rr{'state'} = 'Realm State';
$rr{'user'} = 'User';
$realms{$HEADER_RRN} = \%rr;
}
#-----------------------------------------------------------------------------
# Routine: showrealms()
#
# Purpose: This routine displays realm data. There are three output
# formats: default, long, terse.
# See the pod for more info on the formats.
#
sub showrealms
{
my $out; # Output string.
my $state; # Realm's state.
my $name; # Realm record name.
my $cdir; # Configuration directory.
my $rdir; # Directory for realm's files.
my $admin; # Realm administrator's email.
my $disp; # Display flag.
my $rollrec; # Rollrec file for realm.
my $manager; # Rollover manager.
my $args; # Manager's arguments.
my $user; # User to run as.
#
# Loop through the realm list and give data on the desired realms.
#
foreach my $rrn (sort(keys(%realms)))
{
my $rrr = $realms{$rrn}; # Realm's loaded realm ref.
my %rr = %$rrr; # Realm's loaded realm.
#
# Bump the matching-records count.
#
$count++ if($rrn ne $HEADER_RRN);
#
# Stay cloaked if only the count of matching records
# should be given.
#
next if($cntflag);
#
# Set fields from the realm.
#
$state = $rr{'state'};
$admin = $rr{'administrator'};
$args = $rr{'args'};
$cdir = $rr{'configdir'};
$disp = $rr{'display'};
$manager = $rr{'manager'};
$name = $rr{'realm_name'};
$rdir = $rr{'realmdir'};
$rollrec = $rr{'rollrec'};
$state = $rr{'state'};
$user = $rr{'user'};
#
# Build the output string.
#
$prevkey = 'first-field';
$out = outfld($rrn,'realm_name',$name,1);
$out .= outfld($rrn,'state',$state,$stateflag);
$out .= outfld($rrn,'realmdir',$rdir,$realmdirflag);
$out .= outfld($rrn,'rollrec',$rollrec,$rollrecflag);
$out .= outfld($rrn,'configdir',$cdir,$configdirflag);
$out .= outfld($rrn,'administrator',$admin,$adminflag);
$out .= outfld($rrn,'manager',$manager,$managerflag);
$out .= outfld($rrn,'display',$disp,$dispflag);
$out .= outfld($rrn,'user',$user,$userflag);
$out .= outfld($rrn,'args',$args,$argsflag);
#
# Write the output.
#
print "$out\n";
}
}
#----------------------------------------------------------------------
# Routine: outfld()
#
# Purpose: Build a field's piece of the output line. It does the needed
# spacing and determination of whether that fields should be
# included in the line.
#
# This routine only *builds* part of the output line.
# It does not write it to output.
#
sub outfld
{
my $realm = shift; # Realm name.
my $key = shift; # Hashkey.
my $val = shift; # Value to print.
my $flag = shift; # Output flag.
my $ret; # Return string.
my $mlen; # Maximum field length.
my $vlen; # Length of value.
my $blen; # Space buffer length.
my $spaces; # Spaces to add.
#
# Return if the given flag isn't set and -long wasn't given.
#
return('') if(!$long && !$flag);
#
# Figure out spacing for this line.
#
$mlen = $lengths{$prevkey};
$vlen = length($realms{$realm}{$prevkey});
$blen = $mlen - $vlen;
$spaces = ' ' x $blen if($blen > 0);
#
# Build the output line.
#
$ret = $spaces . $val;
#
# Save the hash key and return the output string to the caller.
#
$prevkey = $key;
return($ret);
}
#----------------------------------------------------------------------
# Routine: getpath()
#
# Purpose: Return the path to print, based on if the -fullpaths option
# was given. If it was, the full path will be used. If not,
# the final path node will be used.
#
sub getpath
{
my $path = shift; # Path to massage.
if((! $fullpaths) && ($path =~ /\//))
{
my @nodes; # Path chunks.
@nodes = split('/',$path);
$path = $nodes[-1];
}
return($path);
}
#----------------------------------------------------------------------
# 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: lsrealm [options] <realm-file>\n";
print STDERR "\trecord-selection options:\n";
print STDERR "\t\t-active active records\n";
print STDERR "\t\t-inactive inactive records\n";
print STDERR "\tattribute-selection options:\n";
print STDERR "\t\t-name name\n";
print STDERR "\t\t-state realm's state\n";
print STDERR "\t\t-realmdir realm's directory\n";
print STDERR "\t\t-configdir configuration directory\n";
print STDERR "\t\t-rollrec rollrec file\n";
print STDERR "\t\t-admin realm adminstrator's email\n";
print STDERR "\t\t-display display flag\n";
print STDERR "\t\t-manager realm's rollover manager\n";
print STDERR "\t\t-args manager arguments\n";
print STDERR "\t\t-user manager user\n";
print STDERR "\toutput-format options:\n";
print STDERR "\t\t-count only give count of matching realms\n";
print STDERR "\t\t-fullpaths only give count of matching realms\n";
print STDERR "\t\t-headers give explanatory column headers\n";
print STDERR "\t\t-terse terse output\n";
print STDERR "\t\t-long long output\n";
print STDERR "\t\t-Version display version number\n";
print STDERR "\t\t-help help message \n";
exit(1);
}
1;
##############################################################################
#
=pod
=head1 NAME
lsrealm - List the I<realm>s in a DNSSEC-Tools I<realm> file
=head1 SYNOPSIS
lsrealm [options] <realm-files>
=head1 DESCRIPTION
This script lists the contents of the specified I<realm> files. All I<realm>
files are loaded before the output is displayed. If any I<realm>s have
duplicated names, whether within one file or across multiple files, the later
I<realm> will be the one whose data are displayed.
The output displayed for each realm in a I<realm> file depends on the selected
records, the selected attributes, and the selected output format. Each option
in these option groups is described in detail in the next section. The three
base output formats are described here.
The B<-terse> option indicates that a minimal amount of output is desired; the
B<-long> option indicates that a great deal of output is desired. The
record-selection and attribute-selection options may be used in conjunction
with B<-terse> to display exactly the set of I<realm> fields needed.
The default output format is that used when neither B<-terse> nor B<-long> is
given, and is a middle ground between terse and long output.
The table below shows the fields displayed for each output format.
realm field default terse long
------------- ------- ----- ----
realm name yes yes yes
state yes no yes
config directory no no yes
realm directory no no yes
rollrec file no no yes
administrator no no yes
directory no no yes
display flag no no yes
manager no no yes
manager arguments no no yes
user no no yes
=head1 OPTIONS
There are three types of options recognized by B<lsrealm>: record-selection
options, attribute-selection options, and output-format options. Each type
is described in the subsections below.
=head2 Record-selection Options
These options select the records that will be displayed by B<lsrealm>.
By default, all records will be displayed; selecting one or the other of
these options will restrict the records shown.
These options are mutually exclusive.
=over 4
=item B<-active>
List all active records in the I<realm> file.
=item B<-inactive>
List all inactive records in the I<realm> file.
=back
=head2 Attribute-selection Options
These options select the attributes of the records that will be displayed
by B<lsrealm>.
=over 4
=item B<-admin>
The record's administrator value is included in the output.
If an administrator value is not included in a I<realm>, then the value
"(defadmin)" will be given.
=item B<-args>
The record's user-defined arguments for the realm's rollover manager.
=item B<-configdir>
The record's configuration directory is included in the output.
This value will be the filename only, unless the B<-fullpaths> option
is given. In that case, the full pathname will be printed.
=item B<-display>
The record's display flag, used by B<grandvizier> is included in the output.
=item B<-manager>
The record's manager field, indicating which rollover manager is used for the
realm.. The default is B<rollerd>, but individual installations may use other
managers.
=item B<-realmdir>
The record's realm directory is included in the output.
This value will be the filename only, unless the B<-fullpaths> option
is given. In that case, the full pathname will be printed.
=item B<-rollrec>
The record's I<rollrec> file is included in the output.
This value will be the filename only, unless the B<-fullpaths> option
is given. In that case, the full pathname will be printed.
=item B<-state>
Include each I<realm> record's state in the output. The state will be
either "active" or "inactive".
This field is part of the default output.
=item B<-user>
The record's user field, which is the user the realm's manager will be
executed with. I<This field is not yet implemented.>
=back
=head2 Output-format Options
These options select the type of output that will be given by I<lsrealm>.
=over 4
=item B<-count>
Only a count of matching realms in the I<realm> file is given.
=item B<-fullpaths>
Display pathname data as the full pathname given in the realm file.
If this option is not specified, only the filename itself will be displayed.
=item B<-headers>
Display explanatory column headers.
=item B<-terse>
Terse output is given. Only the record name and any other fields specifically
selected are included in the output.
=item B<-long>
Long output is given. All record fields are included.
=item B<-help>
Display a usage message.
=item B<-Version>
Displays the version information for B<lsrealm> and the DNSSEC-Tools package.
=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<grandvizier(8)>,
B<realminit(8)>
B<Net::DNS::SEC::Tools::realm.pm(3)>
B<file-realm(5)>
=cut
|