#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux
# Copyright (c) 1999, 2000, 2001, 2002 Networks Associates
# Technology, Inc.  All rights reserved.
# 
# This file is part of LOMAC.
#
# LOMAC is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# LOMAC 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 LOMAC; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# testwrite2
#
# This script tests LOMAC's handling of the following system calls:
#    sys_write()
# This script tests the use of sys_write on FIFOs.
#
# This should be run at level 2.
#
#########################################################################

use POSIX;
use lomacpred;

$lowfilename = "/tmp/testwrite2lowfile";
$lowfifoname = "/tmp/testwrite2lowfifo";
$highfifoname = "/root/testwrite2highfifo";
$message = "this is the message";
$message_length = 19;

#
# 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";
print( "*** Testing sys_write(fifo). ***\n" );

unlink( $lowfilename );
unlink( $lowfifoname );
unlink( $highfifoname );

#
# create a low-level file
#
print( "Creating low-level file:\n" );
open( LOWFILE, "> $lowfilename" ) || die "can't create $lowfilename: $!\n";
print LOWFILE "garbage\n";
close LOWFILE;
if( FileAtLevelPred( $lowfilename, 1 ) ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}
    
# create high fifo
print( "Create high FIFO:\n" );
if( system( "mkfifo $highfifoname" ) == 0 ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

# create low FIFO
print( "Create low FIFO:\n" );
if( system( 'mkfifo', $lowfifoname ) == 0 ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}


#
# create first child
#
$child_pid = fork;
if( ! defined $child_pid ) {
    die "fork: $!\n";
}
if( $child_pid == 0 ) {
    # first child
    setpgrp( 0, $$ );  # put child into its own pgrp
    
    # read from high and low FIFOs
    open( THEFIFO, "<$highfifoname" ) || die "open $highfifoname: $!\n";
    $garbage = <THEFIFO>;
    close( THEFIFO );
    open( THEFIFO, "<$lowfifoname" ) || die "open $lowfifoname: $!\n";
    $garbage = <THEFIFO>;
    close( THEFIFO );
    
    exit( 0 );

} # end of first child

# parent here,

#
# Test high process write to a high-level fifo   
#
print( "Test high process write to high fifo:\n" );
$highfd = POSIX::open( $highfifoname, POSIX::O_WRONLY );
if( ! ( defined $highfd ) ) {
    die( "can't open $highfifoname: $!\n" );
}
$bytes = POSIX::write( $highfd, $message, $message_length );
if( defined $bytes ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}
POSIX::close( $highfd );

#
# test high process write to a low-level fifo    
#

# Test high process write to a low-level fifo   
print( "Test high process write to low fifo:\n" );
$lowfd = POSIX::open( $lowfifoname, POSIX::O_WRONLY );
if( ! ( defined $lowfd ) ) {
    die( "can't open $lowfifoname: $!\n" );
}
$bytes = POSIX::write( $lowfd, $message, $message_length );
if( defined $bytes ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}
POSIX::close( $lowfd );

# wait for child to exit
$ret_val = waitpid( $child_pid, 0 );


#
# create second child
#
$child_pid = fork;
if( ! defined $child_pid ) {
    die "fork: $!\n";
}
if( $child_pid == 0 ) {
    # second child
    setpgrp( 0, $$ );  # put child into its own pgrp

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

	# grandchild here

	# demote grandchild
	open( LOWFILE, "<$lowfilename" ) || die "open $lowfilename: $!\n";
	$garbage = <LOWFILE>;
	if( !( &ProcAtLevelPred( 1 ) ) ) {
	    die "grandchild not demoted properly\n";
	}
	close LOWFILE;

	# Test low process write to a low-level fifo   
	$lowfd = POSIX::open( $lowfifoname, POSIX::O_WRONLY );
	if( ! ( defined $lowfd ) ) {
	    die( "can't open $lowfifoname: $!\n" );
	}

	print( "Test low process write to low fifo:\n" );
	$bytes = POSIX::write( $lowfd, $message, $message_length );
	if( defined $bytes ) {
	    print( "\tOK.\n" );
	} else {
	    print( "\tFAILED.\n" );
	    exit( -1 );
	}
      POSIX::close( $lowfd );


	# Test low process write to a high-level fifo   
	print( "Test low process write to high fifo:\n" );
	$highfd = POSIX::open( $highfifoname, POSIX::O_WRONLY );
	if( ! ( defined $highfd ) ) {
	    die( "can't open $highfifoname: $!\n" );
	}
	
	$bytes = POSIX::write( $highfd, $message, $message_length );
	if( !( defined $bytes ) ) {
	    print( "\tOK.\n" );
	} else {
	    print( "\tFAILED.\n" );
	    exit( -1 );
	}
      POSIX::close( $highfd );
	
	exit( 0 );
	
    } # end of grandchild

    # second child here

    open( LOWFIFO, "<$lowfifoname" ) || die "open $lowfifoname: $!\n";
    open( HIGHFIFO, "<$highfifoname" ) || die "open $highfifoname: $!\n";
    $garbage = <LOWFIFO>;
    close( LOWFIFO );
    close( HIGHFIFO );

    # wait for grandchild to exit
    $ret_val = waitpid( $grandchild_pid, 0 );

    exit( 0 );
} # end of second child

# parent here

# wait for child to exit
$ret_val = waitpid( $child_pid, 0 );

# clean up
unlink( $lowfilename );
unlink( $lowfifoname );
unlink( $highfifoname );

exit( 0 );
