#!/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.
#
#
# testread4
#
# This script tests LOMAC's handling of the following system calls:
#    sys_read()
# This script tests the use of sys_read when used to read from UNIX
# (local) domain sockets.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$lowfilename = "/tmp/testread3lowfile";
$lowudsname  = "/tmp/testread3lowuds";
$highudsname = "/root/testread3highuds";
$messageone   = "this is message one";
$messagetwo   = "this is message two";
$messagethree = "this is message three";
$messagefour  = "this is message four";


#
# 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_read(UNIX domain socket). ***\n" );

#
# create a low-level file
#
print( "Creating low-level file:\n" );
unlink( $lowfilename );
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 unix-domain socket
print( "Create high UNIX-domain socket:\n" );
$uaddr = sockaddr_un( $highudsname );
$proto = getprotobyname( 'tcp' );
socket( ServerHighSocket, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!\n";
unlink( $highudsname );
if( bind( ServerHighSocket, $uaddr ) ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

# create low unix-domain socket
print( "Create low UNIX-domain socket:\n" );
$uaddr = sockaddr_un( $lowudsname );
$proto = getprotobyname( 'tcp' );
socket( ServerLowSocket, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!\n";
unlink( $lowudsname );
if( bind( ServerLowSocket, $uaddr ) ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

#
# Create first child.
#
$child_pid = fork;
if( !defined $child_pid ) {
    die "can't fork: $!\n";
}

if( $child_pid == 0 ) {
    # beginning of first child

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

    # Test read from high UNIX domain socket
    print "Test high process read from high UNIX-domain socket:\n";
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!\n";
    connect( SOCK, sockaddr_un( $highudsname ) ) || die "connect: $!\n";
    $fromuds = <SOCK>;
    if( ( $fromuds eq $messageone ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( SOCK );

    # Test read from low UNIX domain socket
    print "Test high process read from low UNIX-domain socket:\n";
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!\n";
    connect( SOCK, sockaddr_un( $lowudsname ) ) || die "connect: $!\n";
    $fromuds = <SOCK>;
    if( ( $fromuds eq $messagetwo ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( SOCK );

    exit( 0 );
} # end of first child


# parent here
listen( ServerHighSocket, SOMAXCONN ) || die "listen $!\n";
accept( ClientSocket, ServerHighSocket ) || die "accept $!\n";
print ClientSocket $messageone;
close ClientSocket;

listen( ServerLowSocket, SOMAXCONN ) || die "listen $!\n";
accept( ClientSocket, ServerLowSocket ) || die "accept $!\n";
print ClientSocket $messagetwo;
close ClientSocket;

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

# cleanup
close ServerHighSocket;
close ServerLowSocket;
unlink( $highudsname );
unlink( $lowudsname );
unlink( $lowfilename );

exit( 0 );

