#!/usr/perl5/bin/perl
#
# File: 
#       /opt/fw/scripts/rreport.pl
#
# Purpose: 
#       read raptor configuration to terminal
#
# Assumptions:
#       raptor config is in /usr/adm/sg and raptor version is 6.5.3
#
# Source: http://www.bastard.net/~kos/rreport.pl
#
# History:
#   
#   10-Nov-2001 fmigge,         adjusted for Oracle raptor firewalls
#
# show group|host|subnet|domain
$show=host;
$raptorroot = "/usr/adm/sg";

sub gethostname {
    my $hostname;
    my $ind;
    
    open(FOO, "</etc/nodename") || return 1;
    $hostname = <FOO>;
    chomp($hostname);
    close(FOO);
    return $hostname;
}

sub parseline {
	my @line;
	my @out;
	my $i;
	my $quoted;
	my $current;

	@line = split(/ /, $_[0]);
	$quoted = 0;
	for($i=0; $i<$#line; $i++) {
	
		if($line[$i] =~ /^\{(.*)/) {	
			$quoted = 1;
			$current = "";
			$line[$i] = $1;
		}
	
		if($line[$i] =~ /(.*)}$/) {
			$quoted = 0;
			$line[$i] = $current.$1;
		}

		if($quoted) {
 			$current .= $line[$i];
			$current .= " ";
			next;
		} else {
			$current = $line[$i];
		}
		
		push(@out, $current);
	}
	
	return @out;
}

sub doentities {
	my $file = shift;
	my @ents;
	my @group;

	# First pass: do hosts
	open(IN, $file) || die "Cant open $file: $!";
	while(<IN>) {
		chomp;
		@group = ();
		@ents = parseline($_);
		if($ents[1] eq "host") {
			$entity{$ents[0]} = $ents[3];
			$desc{$ents[0]} = $ents[2];
			if ( $show eq "host" ) {
				print " $ents[0] \t-";
				print " $ents[3] \t-";
				print " $ents[2] \n";
			}
		}
		if($ents[1] eq "subnet") {
			$entity{$ents[0]} = $ents[3];
			$desc{$ents[0]} = $ents[2];
			if ( $show eq "subnet" ) {
				print " $ents[0] \t-";
				print " $ents[3] \t-";
				print " $ents[2] \n";
			}
		}
		if($ents[1] eq "secure") {
			$entity{$ents[0]} = $ents[3];
			$desc{$ents[0]} = $ents[2];
		}
		if($ents[1] eq "domain") {
			$entity{$ents[0]} = $ents[3];
			$desc{$ents[0]} = $ents[2];
			if ( $show eq "domain" ) {
				print " $ents[0] \t-";
				print " $ents[3] \t-";
				print " $ents[2] \n";
			}
		}
	}
	close(IN);
	
	# Second pass: do groups
	open(IN, $file) || die "Cant open $file: $!";
	while(<IN>) {
		chomp;
		@group = ();
		@ents = parseline($_);
        	if ( $show eq "group" ) {
			if($ents[1] eq "group") {
				print "==> Group $ents[0]:\n";
				@group = split(/ /, $ents[5]);
				for($i=0; $i<=$#group; $i++) {
					print "$group[$i] \t- ";
					print "$entity{$group[$i]} \t- ";
					print "$desc{$group[$i]}\n";
				}
				print "==> End of Group $ents[0].\n";
				# $entity{$ents[0]} = join(", ", @group);
			}
		}
	}
	close(IN);
}


#
# main
#

$raptorroot = $ARGV[0] unless ($#ARGV==-1);

stat($raptorroot) || die "The raptor main directory $raptorroot does not exist. Try passing it as an argument";

doentities($raptorroot."/pkentity");

#while(<>) {
#	chomp;
#print join("|", parseline($_));
#	print "\n";
#}

