#!/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
# non-UNIX (non-local) domain sockets.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$serverport = 28376;
$proto = getprotobyname( 'tcp' );
$message = "this is the message";


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

# make parent a server on localhost $serverport
socket( ServerSocket, PF_INET, SOCK_STREAM, $proto ) || die "socket: $!\n";
bind( ServerSocket, sockaddr_in($serverport,INADDR_ANY) ) || die "bind: $!\n";

#
# create a child to be a client
#
$child_pid = fork;
if( !defined $child_pid ) {
    die "can't fork: $!\n";
}

if( $child_pid == 0 ) {
    # beginning of 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 );
    }

    # create an INET socket
    $serverhostname = 'localhost';
    $iaddr = inet_aton( $serverhostname ) || die "can't lookup IP: $!\n";
    $paddr = sockaddr_in( $serverport, $iaddr );
    socket( SOCK, PF_INET, SOCK_STREAM, $proto ) || die "socket: $!\n";
    connect( SOCK, $paddr ) || die "connect: $!\n";

    print "Test read from INET socket:\n";
    $fromsock = <SOCK>;
    if( ( $fromsock = $message ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    close( SOCK );
    exit( 0 );

} # end of child

#
# parent here
#

listen( ServerSocket, SOMAXCONN ) || die "listen: $!\n";
accept( ClientSocket, ServerSocket ) || die "accept: $!\n";
print ClientSocket $message;
close( ClientSocket );
close( ServerSocket );

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

exit( 0 );
