#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (C) 2000 NAI Labs.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.  This program
# 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 this program; if not, write
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#
# testbind1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_socketcall(BIND)
# This script tests the use of sys_socketcall when used to bind UNIX
#    (local) domain sockets to names in the filesystem.
#
# This should be run at level 2.
#
#########################################################################

use Socket;
use lomacpred;

$lowfilename = "/tmp/testbind1lowfile";
$lowsocketname  = "/tmp/testbind1lowsocket";
$highsocketname = "/root/testbind1highsocket";


#
# 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(bind). ***\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 );
}
    

#
# Create a child.
#
if( $child_pid = fork ) {
    #parent

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

    # clean up
    unlink( $lowfilename );
    unlink( $lowsocketname );
    unlink( $highsocketname );

    exit( $ret_val );

} elsif( $child_pid == 0 ) {
    #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 high proc binds to high socket
    print( "Test high process binding high UNIX domain socket:\n" );
    $uaddr = sockaddr_un($highsocketname);
    socket( Server, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!";
    if( bind( Server, $uaddr ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    close( Server );

    # test high proc binds to low SOCKET
    print( "Test high process binding low UNIX domain socket:\n" );
    $uaddr = sockaddr_un($lowsocketname);
    socket( Server, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!";
    if( bind( Server, $uaddr ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( Server );


    # clean up for round two...
    unlink( $highsocketname );
    unlink( $lowsocketname );


    # demote ourselves
    print( "Demote ourself:\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 );
    }

    # test low proc binds high UNIX domain socket (should fail)
    print( "Test low process binding high UNIX domain socket:\n" );
    $uaddr = sockaddr_un($highsocketname);
    socket( Server, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!";
    if( !( bind( Server, $uaddr ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( Server );

    # test low proc binds low UNIX domain socket
    print( "Test high process binds low UNIX domain socket:\n" );
    $uaddr = sockaddr_un($lowsocketname);
    socket( Server, PF_UNIX, SOCK_STREAM, 0 ) || die "socket: $!";
    if( bind( Server, $uaddr ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    close( Server );

    exit( 0 );

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

