#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (c) 1999-2001 NAI Labs, Inc.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
#   Redistributions of source code must retain the above copyright
#   notice, this list of conditions and the following disclaimer.
# 
#   Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
# 
#   Neither the name of NAI Labs, Inc. nor the names of its contributors
#   may be used to endorse or promote products derived from this software
#   without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# testlink
#
# This script tests LOMAC's handling of the following system calls:
#    sys_link()
#
# This should be run at level 2, as root.
#
#########################################################################

use lomacpred;

#
# Ensure that LOMAC is running and that we are running at level 2.
#
&ProcAtLevelPred( 2 ) || die "ERROR: You must run this test at level 2.\n";
( 0 == $EUID ) || die "ERROR: You must run this test as root.\n";

print( "*** Testing sys_link(). ***\n" );

$lowfilename_one = "/tmp/testlinklowfile1";
$highfilename_one = "/root/testlinkhighfile1";
$lowfilename_two = "/tmp/testlinklowfile2";
$highfilename_two = "/root/testlinkhighfile2";

#
# create low and high files
#
unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );
open( HIGHFILE, ">$highfilename_one" ) || die "open $highfilename_one: $!\n";
print HIGHFILE "garbage";
close( HIGHFILE );
open( LOWFILE, ">$lowfilename_one" ) || die "open $lowfilename_one: $!\n";
print LOWFILE "garbage";
close( LOWFILE );

#
# Create a child to run high-process link tests
#

$child_pid = fork;
if( !( defined $child_pid ) ) {
    die "fork: $!\n";
}

if( $child_pid == 0 ) {
    # child here

    # Put child in its own new pgrp.
    print( "Putting child into its own pgrp:\n" );
    setpgrp( 0, $$ );                   # put child in its own pgrp
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    #
    # do high child link tests:
    #

    print( "Test high process link high file to high file:\n" );
    $ret_val = link( $highfilename_one, $highfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process link low file to low file:\n" );
    $ret_val = link( $lowfilename_one, $lowfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process link high file to low file:\n" );
    $ret_val = link( $highfilename_two, $lowfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process link low file to high file:\n" );
    $ret_val = link( $lowfilename_two, $highfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} # done with child for high-process tests

# parent here

#
# wait for child to exit
#

waitpid( $child_pid, 0 );

#
# create new files for next round of tests
#

unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );
open( HIGHFILE, ">$highfilename_one" ) || die "open $highfilename_one: $!\n";
print HIGHFILE "garbage";
close( HIGHFILE );
open( LOWFILE, ">$lowfilename_one" ) || die "open $lowfilename_one: $!\n";
print LOWFILE "garbage";
close( LOWFILE );

#
# fork a child to do low-process link tests.
#

$child_pid = fork;
if( !( defined $child_pid ) ) {
    die "fork: $!\n";
}

if( $child_pid == 0 ) {
    # child here

    # Put child in its own new pgrp.
    print( "Putting child into its own pgrp:\n" );
    setpgrp( 0, $$ );                   # put child in its own pgrp
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    #
    # demote child
    #
    print( "Demoting child:\n" );
    open( LOWFILE, "+< $lowfilename_one" ) ||
	die "child can't open $lowfilename_one: $!\n";
    print LOWFILE "garbage";
    $garbage = <LOWFILE>;
    close( LOWFILE );
    if( &ProcAtLevelPred( 1 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    #
    # do low child link tests:
    #

    print( "Test low process link high file to high file:\n" );
    $ret_val = link( $highfilename_one, $highfilename_two );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process link low file to low file:\n" );
    $ret_val = link( $lowfilename_one, $lowfilename_two );
    if( $ret_val == 1 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process link high file to low file:\n" );
    $ret_val = link( $highfilename_two, $lowfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process link low file to high file:\n" );
    $ret_val = link( $lowfilename_two, $highfilename_one );
    if( $ret_val == 0 ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} # done with child for low-process tests

# wait for child
waitpid( $child_pid, 0 );

# clean up
unlink( $lowfilename_one );
unlink( $highfilename_one );
unlink( $lowfilename_two );
unlink( $highfilename_two );

exit( 0 );


