#!/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
#
#
# 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 );

