#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (C) 1999 TIS Labs at Network Associates, Inc.
# Copyright (C) 2000 - 2001 NAI Labs
# Copyright (C) 2001 John Thiltges
# Copyright (C) 2002 Networks Associates Technology, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.  This program
# is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.  You should have received a copy of the
# GNU General Public License along with this program; if not, write
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#
# lls - a script which calls ls -al, and prints the level of a given
#       file after its gid in ls's output.
#
# USAGE:  "lls <path>" or "lls"
#
# Tim Fraser <tfraser@tislabs.com>        : initial implementation
# John Thiltges <jthiltg1@bigred.unl.edu> : adapted to use lomac.pm
#
#########################################################################

use lib "/opt/lomac/bin";
use lomac;

#
# Step 0 - Make sure LOMAC is operational.
#

if (linfo("-c")) {
    die "LOMAC not operating.\n";
}

#
# Step 1 - Handle arguments.  We expect either:
#   lls                        or
#   lls <path>                 .  
#

if( $#ARGV > 0 ) {
    die "USAGE: lls <path>\n       lls\n";
}

if( $#ARGV == 0 ) {
    # put $ARGV[ 0 ] into $path.
    $path = $ARGV[ 0 ];
} else {
    # this is the zero command-line argument case in which we assume
    # "lls .".  Make path the absolute path to ".".
    open( PWDOUT, "pwd|" ) || die "Can't execute pwd\n";
    $path = <PWDOUT>;
    chop( $path );
}

#
# Step 2 - There is no step two.
#


#
# Step 3 - Execute ls -al with our command line arguments and put its
#          output into <LSOUT>.
#

# Execute /bin/ls and put its output into <LSOUT>.
open( LSOUT, "/bin/ls -al $path|" ) || die "Can't execute /bin/ls";


#
# Step 4 - Process the output of /bin/ls.  If $path is a file, determine
#          its level and produce a simple one-line output.  If $path
#          is a directory, we must do processing for every file it contains.
#

if( !(  -d $path ) ) {

    # $path is not a directory, produce simple one-line output.
    # non-existant paths wind up here too, since they aren't directories.

    $lsline = <LSOUT>;

    # quit if ls produced no output (nonexistent path, no permission, etc.)
    if( $lsline eq "" ) {
	exit( -1 );
    }

    # split $lsline into @lscolumns.  The columns we're interested in are:
    # [ 0 ] permission bits
    # [ 2 ] uid
    # [ 3 ] gid
    # [ last ] filename
    @lscolumns = split( / +/, $lsline );

    # determine level of file named by $path
    $level = level("-f",$path);
    
    print @lscolumns[ 0 ], "   ";
    print sprintf( "%-12s%-12s", @lscolumns[ 2 ], @lscolumns[ 3 ] );
    print sprintf( "%3u   ", $level );
    print @lscolumns[ $#lscolumns ];

} else {

    # $path is a directory.  Put the output in <LSOUT> into the @lsout
    # array.  Iterate through the lines of @lsout, selecting the
    # columns we want to output, and interleaving our level
    # information in the proper places.

    @lsout = <LSOUT>;

    # The first line of /bin/ls -al output is some sort of total line.
    # We'll skip it using splice().
    foreach $lsline ( splice( @lsout, 1 ) ) {

	# split $lsline into @lscolumns.  The columns we're interested in are:
	# [ 0 ] permission bits
	# [ 2 ] uid
	# [ 3 ] gid
	# [ last ] filename
	@lscolumns = split( / +/, $lsline );

	# determine level of file based on name in @lscolumns[ $#lscolumns ]
	$filename = $path;
	$component = @lscolumns[ $#lscolumns ];
	chop( $component );    # remove trailing "\n"
	$filename .= "/";
	$filename .= $component;

#	print "looking up ", $filename, "[", $component , "]\n";
	$level = level("-f",$filename);

	print @lscolumns[ 0 ], "   ";
	print sprintf( "%-12s%-12s", @lscolumns[ 2 ], @lscolumns[ 3 ] );
	print sprintf( "%3u   ", $level );
	print @lscolumns[ $#lscolumns ];

    }

    exit( 0 );
}




