#!/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
#
#
# testsocketrw1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_read(socket)
#    sys_write(socket)
# This script tests the use of read and write on STREAM UNIX domain
# sockets.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$lowfilename = "/tmp/testsocketrw1lowfile";
$lowsocketname  = "/tmp/testsocketrw1lowsocket";
$highsocketname = "/root/testsocketrw1highsocket";

$message1 = "Blossom..";
$message2 = "Bubbles..";
$message3 = "Buttercup";
$message_length = 9;

#
# 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_socketcall(STREAM UNIX sockets). ***\n" );

unlink( $lowfilename );
unlink( $lowsocketname );
unlink( $highsocketname );


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

#
# Fork off high server
#

if( 0 == ( $highserver_pid = fork ) ) {
    # high server
    setpgrp( 0, $$ );  # Put high server into its own new pgrp.

    # bind high server socket
    $uaddr = sockaddr_un($highsocketname);
    socket( HighServer, PF_UNIX, SOCK_STREAM, 0 ) || die "high socket: $!";
    bind( HighServer, $uaddr ) || die "high bind: $!";

    # handle connections
    while( 1 ) {
	listen( HighServer, SOMAXCONN );
	accept( HighClient, HighServer ) || die "high accept: $!";
	read( HighClient, $temp, $message_length );
	syswrite( HighClient, $temp, $message_length );
	close( HighClient );
    }
    
} # high server



#
# Fork off low server
#

if( 0 == ( $lowserver_pid = fork ) ) {
    # low server
    setpgrp( 0, $$ );  # Put low server into its own new pgrp.

    # bind low server socket
    $uaddr = sockaddr_un($lowsocketname);
    socket( LowServer, PF_UNIX, SOCK_STREAM, 0 ) || die "low socket: $!";
    bind( LowServer, $uaddr ) || die "low bind: $!";

    # handle connections
    while( 1 ) {
	listen( LowServer, SOMAXCONN );
	accept( LowClient, LowServer ) || die "low accept: $!";
	read( LowClient, $temp, $message_length );
	syswrite( LowClient, $temp, $message_length );
	close( LowClient );
    }
    
} # low server




#
# Fork off client
#

if( 0 == ( $client_pid = fork ) ) {
    # client

    setpgrp( 0, $$ );  # Put client into its own new pgrp.

    print( "Test high client W/R to high sever:\n" );
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "client socket: $!";
    connect( SOCK, sockaddr_un( $highsocketname ) ) ||die "client connect: $!";
    # print SOCK $message1";
    # $back = <SOCK>;
    syswrite SOCK, $message1, $message_length;
    read( SOCK, $back, $message_length );
    close( SOCK );
    if( $message1 eq $back ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # this should transfer data, but should demote client on read. 
    print( "Test high client W/R to low sever:\n" );
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "client socket: $!";
    connect( SOCK, sockaddr_un( $lowsocketname ) ) ||die "client connect: $!";
    syswrite SOCK, $message2, $message_length;
    read( SOCK, $back, $message_length );
    close( SOCK );
    if( ( $message2 eq $back ) && ( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }


    print( "Test low client write to high sever:\n" );
    socket( SOCK, PF_UNIX, SOCK_STREAM, 0 ) || die "client socket: $!";
    connect( SOCK, sockaddr_un( $highsocketname ) ) ||die "client connect: $!";
    if( undef == syswrite( SOCK, $message3, $message_length ) ) {
#   if( !( print SOCK $message3 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );
}

#
# Overall parent program.
#

# wait for client to exit.
$ret_val = waitpid( $client_pid, 0 );

# clean up
kill 'TERM', $highserver_pid;
kill 'TERM', $lowserver_pid;

unlink( $lowfilename );
unlink( $lowsocketname );
unlink( $highsocketname );

exit( $ret_val );
