#!/usr/bin/perl
########################################################################
#
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (C) 1999 TIS Labs at Network Associates, Inc.
# Copyright (C) 2000 NAI Labs
#
# 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.
#
#
# level - a perl script to determine the LOMAC level associated with
#         a specified process or file.  Uses the /dev/lomac pseudo-
#         device to retrieve this information from the LOMAC loadable
#         kernel module (LKM).
#
########################################################################

#
# Step 3 - Process command-line arguments
#

# Bomb if we didn't get the proper number of arguments
if( $#ARGV != 0 ) {
    die "USAGE: level <pathname>\n";
}


#
# Step 4 - open "/dev/lomac" and make the appropriate ioctl.
# 

# Open /dev/lomac, the pseudodevice that allows us to query the LOMAC LKM.
open( LOMAC, "/dev/lomac" ) || die "Couldn't open /dev/lomac";

$path = $ARGV[ 0 ];

$ioctlarg[ 0 ] = length( $path ) + 1;   # add 1 for null terminator.
$ioctlarg[ 1 ] = $path;
$ret_val = ioctl( LOMAC, 1, pack( "Ia*", @ioctlarg ) ) || ($ret_val = -1)
    or $ret_val = -1;   # this "or" is needed when ioctol returns undefined

#
# Step 5 - report results.
#

if( $ret_val eq "0 but true" ) {
    exit( 0 );
} else {
    print "Error: ", $!, "\n";
    exit( $! );
}
