#!/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
#
#
# testopen1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_open()
#
# This script tests LOMAC's handling of sys_open when the O_CREAT
# flag is set.
#
# This should be run at level 2, as root.
#
#########################################################################

use lomacpred;
use POSIX;

#
# 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_open( O_CREAT ). ***\n" );

$lowfilename = "/tmp/testopenlowfile";
$highfilename = "/root/testopenhighfile";

#
# Create a child.
#
$ret_val = -1;
if( $child_pid = fork ) {
    #parent

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

    # remove high and low files
    unlink( $lowfilename );
    unlink( $highfilename );

} elsif( $child_pid == 0 ) {
    #child

    # 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 );
    }

    print( "Test high job open/creat high file:\n" );
    $fd = POSIX::open( $highfilename, &POSIX::O_CREAT | &POSIX::O_RDWR, 0611);
    if( ( defined $fd ) &&
	( FileAtLevelPred( $highfilename, 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $fd );
    unlink( $highfilename );

    print( "Test high job open/creat low file:\n" );
    $fd = POSIX::open( $lowfilename, &POSIX::O_CREAT | &POSIX::O_RDWR, 0611 );
    if( ( defined $fd ) &&
	( FileAtLevelPred( $lowfilename, 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::write( $fd, "garbage", 7 );
    POSIX::close( $fd );

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


    print( "Test low job open/creat high file:\n" );
    $fd = POSIX::open( $highfilename, &POSIX::O_CREAT | &POSIX::O_RDWR, 0611);
    if( !( defined $fd ) ) {
	print( "\tOK.\n" );
    } else {
	print( "fd is ", $fd, "\n" );
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low job open/creat low file:\n" );
    $fd = POSIX::open( $lowfilename, &POSIX::O_CREAT | &POSIX::O_RDWR, 0611 );
    if( ( defined $fd ) &&
	( FileAtLevelPred( $lowfilename, 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    POSIX::close( $fd );
    unlink( $lowfilename );

    exit( 0 );

} else {
    die "ERROR: unable to fork\n";
}


exit( $ret_val );


