#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,2016 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)49       1.6  src/hmc/ServiceRM/utils/addrmcacl, ServiceRM, rsct_roota, rootabase 9/12/05 13:14:07

if ( ($#ARGV<2) || ( ($#ARGV>=2) && ($#ARGV % 2 !=0) ) )
{
  print <<FNUSAGETEXT;
usage : addrmcacl <Class> <Userdesc> <Permissions> [ <Userdesc> <Permissions> ... ]

FNUSAGETEXT
exit 1;
}

# sample input is:
# addrmcacl IBM.Foo     joe@LOCALHOST rw
#           $ClassName  $UserDesc[0]  $Perms[0]
# addrmcacl IBM.Foo     joe@LOCALHOST rw         jane@somehost r
#           $ClassName  $UserDesc[0]  $Perms[0]  $UserDesc[1]  $Perms[1]

# get the class name specified
my $ClassName = $ARGV[0];

# get the pairs of user/permissions
my @UserDesc = ();
my @Perms    = (); 

for (my $i=0, my $j=1; $j<=$#ARGV; $j=$j+2, $i++) {

   $UserDesc[$i] = $ARGV[$j];
   $Perms[$i]    = $ARGV[$j+1];
}

# call RSCT command chrmcacls to modify RSCT ACLs file

my $TRUE = 1;
my $FALSE = 0;
my @acl_stanza_lines = ();
my $UPDACL_error = $FALSE;
my $rc = 0;                   

# create the ACL stanza lines that are needed
# first for the Peer Domain class
$acl_stanza_lines[0] = "$ClassName    // $ClassName class - added by addrmcacl\n";

# add the user descriptions and permissions
$j=1;
for ($i=0,$j=1; $i<=$#UserDesc; $i++, $j++) {
   $acl_stanza_lines[$j] = "    $UserDesc[$i]  *   $Perms[$i]  // added by addrmcacl\n";
}

# call chrmcacl using a pipe to pass the ACL info
if ( open(UPDACL,"|/usr/sbin/rsct/install/bin/chrmcacl -a ") ) {

   if ( print UPDACL @acl_stanza_lines ) {

      if (!( close(UPDACL) )) {

         # close failed
         $UPDACL_error = $TRUE;
      }
   }

   # print failed
   else { $UPDACL_error = $TRUE; }
}

# open failed
else { $UPDACL_error = $TRUE; }

# check the return code in case of error from chrmacl
$rc = $?;

# if anything failed, print error message and exit
if ( ($rc != 0) || $UPDACL_error ) {
   print STDERR "addrmcacl encountered an error trying to modify the RSCT ACLs file.\n";
   exit(1);
}