# linuxAddTrustee.pl
#
# by Dean Giles 
# Version 1.0 
# November, 21 2006
#
# This Perl script adds a trustee to a file using the Virtual File Services (VFS) APIs.
# 
# The script requires the name of a user (trustee) complete with the context and tree name.
# The script requires the name of a file, complete with slash and path.
#  
# This script has been designed to be run on Linux. 
# The API set can be downloaded from  http://developer.novell.com/wiki/index.php/Virtual_File_Services_for_NetWare. 
# 
#
# The Perl script requires the name of an NSS volume as input.  It requires that NSS be installed and an NSS volume be present.
#

{
	# User help function
	if (@ARGV < 2 || $ARGV[0] eq "/?" || $ARGV[0] eq "-?") 
	{
		print "USAGE: perl linuxAddTrustee.pl user.context.tree /path/fileName.ext \n\n";
		print "user is the trustee to add.  The name needs to include the full context including the tree.\n";
		print "fileName is the name of the file, it must include the complete path including the leading slash.\n\n";
		print "Example: perl linuxListFileEvents.pl user1.novell.COMPANY_TREE /home/usr/user1/file.txt \n";
		exit;
	}


	# Global Variables
	$file = "+</_admin/Manage_NSS/files.cmd";
	# This is the MSS management file we are accessing
	$reply;	   # Variable used to read from file.
	$command; # This is the NSS xml command that will be the NSS request to the file.
	$name = $ARGV[0];  # This variable holds the trustee name complete with context including the tree.
	$fileName = $ARGV[1];  # This variable holds the file name complete with slash and path.
	$error;  # Variable used to manage errors.

	# Open the file and check that it exists.
	open(NSSFILE, $file) 
  		or die "Error opening NSS management file ($!) on server";
	print "file is now open \n";
	
	# Initializing the virtual file for sending a command.
	
	$command = "<virtualIO><datastream name=\"command\"/></virtualIO>".
		"<fileRequest><trustees><addTrustee>".
		"<name>$name</name>".
		"<rights>fileScan</rights>".
		"<fileName>$fileName</fileName>".
		"</addTrustee></trustees></fileRequest>";
	print "NSS management file Name: $file \n";
	print "Trustee: $name \n";
	print "FileName: $fileName \n";
	print "Request Sent: $command \n"; 

	if (!syswrite(NSSFILE, $command, length($command))) #Initialize the nss management file and write a command.
		{
		
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
		sysread (NSSFILE, $error, 10000);  #Read the error message.
		
		print "Error writing initialization to management file. \n";
		print "$error  \n\n";	 #Print the error message to the screen.
		close (NSSFILE);
		}
	else
		{
		seek NSSFILE, 0, 0;	  # Make sure to start at the beginning of the file.
	
		sysread (NSSFILE, $reply, 10000);  #Read the reply.
	
		print "$reply  \n\n";	 #Print the reply to the screen.
		
		close (NSSFILE);
  		}

	
}
    	
