#!/bin/sh 

#####################################################################
# scenario #4
#     Explores relationship between slice and controls
#
#  Drd, a geniPI, created(owner of) slices 'sliceA' and 'sliceB'
#  Drd grant Frank, a geniUser, controls permission on 'sliceA'
#  Drd grant Dan, a geniUser, controls_ permission on 'sliceA'
#  Drd grant Jim, a geniUser, delegate_controls_ permission on 'sliceA'
#  Jim grant Nancy, a geniUser, controls_ permission on 'sliceA'
#  SA grant Lisa, a geniUser, controls permission on 'sliceA'
#  SA grant Dan, a geniUser, controls_ permission on 'sliceA'
#  SA grant Dan, a geniUser, controls_ permission on 'sliceB'
#  SA grant Cary, a geniUser, delegate_controls_ permission on 'sliceA'
#  SA grant Alice, a geniUser, controls_ permission on 'sliceB'
#  SA grant Alice, a geniUser, controls_ permission on 'sliceA'
#
unset ABAC_CN
keyloc=`pwd`
sidx=s4

makeWho() {
    WHO=$1
    rm -rf ${WHO}_*.pem
    creddy --generate --cn ${WHO}
}
makeWho GR
makeWho IDP
makeWho EXO
makeWho Drd
makeWho Frank
makeWho Dan
makeWho Lisa
makeWho Cary
makeWho Jim
makeWho Nancy
makeWho Alice

g=`creddy --keyid --cert $keyloc/G_ID.pem`
gr=`creddy --keyid --cert $keyloc/GR_ID.pem`
pa=`creddy --keyid --cert $keyloc/PA_ID.pem`
sa=`creddy --keyid --cert $keyloc/SA_ID.pem`
idp=`creddy --keyid --cert $keyloc/IDP_ID.pem`
exo=`creddy --keyid --cert $keyloc/EXO_ID.pem`
drd=`creddy --keyid --cert $keyloc/Drd_ID.pem`
frank=`creddy --keyid --cert $keyloc/Frank_ID.pem`
dan=`creddy --keyid --cert $keyloc/Dan_ID.pem`
lisa=`creddy --keyid --cert $keyloc/Lisa_ID.pem`
cary=`creddy --keyid --cert $keyloc/Cary_ID.pem`
jim=`creddy --keyid --cert $keyloc/Jim_ID.pem`
nancy=`creddy --keyid --cert $keyloc/Nancy_ID.pem`
alice=`creddy --keyid --cert $keyloc/Alice_ID.pem`

pname=${sidx}_yap_clauses
namespace=access1
sfile=sfile_${sidx}

idp_gUser()
{
eval "namename=[keyid:\$$1]"
attr="[keyid:$idp].role:geniUser <- $namename"
name=${sidx}_IDP_gUser__$1_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

## drd requesting a sliceA from ps
## makeSlice $drd sliceA Drd
makeSlice() {
   what="'$2'"
# Drd.slice <- 'sliceA'
   attr="[keyid:$1].oset:slice <- [string:$what]"
   name=${sidx}_$3_slice__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# SA.standard <- 'sliceA'
   attr="[keyid:$sa].oset:standard <- [string:$what]"
   name=${sidx}_SA_standard__$2_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# SA.owner('sliceA') <- Drd
   attr="[keyid:$sa].role:owner([string:$what]) <- [keyid:$1]"
   name=${sidx}_SA_owner_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
# SA.root('sliceA') <-Drd
   attr="[keyid:$sa].role:root([string:$what]) <- [keyid:$1]"
   name=${sidx}_SA_root_$2__$3_attr.der
   creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace
}

###############################################################################
echo "s/$g/G/g "  > $sfile 
echo "s/$gr/GR/g "  >> $sfile 
echo "s/$sa/SA/g "  >> $sfile
echo "s/$sa/SA/g "  >> $sfile
echo "s/$idp/IDP/g "  >> $sfile
echo "s/$exo/EXO/g "  >> $sfile
echo "s/$drd/Drd/g "  >> $sfile
echo "s/$frank/Frank/g "  >> $sfile
echo "s/$dan/Dan/g "  >> $sfile
echo "s/$lisa/Lisa/g "  >> $sfile
echo "s/$cary/Cary/g "  >> $sfile
echo "s/$jim/Jim/g "  >> $sfile
echo "s/$nancy/Nancy/g "  >> $sfile
echo "s/$alice/Alice/g "  >> $sfile

# G.geniRoot <- GR 
attr="[keyid:$g].role:geniRoot <- [keyid:$gr]"
name=${sidx}_G_geniRoot__gr_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.sliceAuthority <- SA
attr="[keyid:$gr].role:sliceAuthority <- [keyid:$sa]"
name=${sidx}_GR_sliceAuthority__sa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.identityProvider <- IDP
attr="[keyid:$gr].role:identityProvider <- [keyid:$idp]"
name=${sidx}_GR_identityProvider__idp_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.projectAuthority <- PA
attr="[keyid:$gr].role:projectAuthority <- [keyid:$pa]"
name=${sidx}_GR_projAuthority__pa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# GR.aggregate <- EXO
attr="[keyid:$gr].role:aggregate <- [keyid:$exo]"
name=${sidx}_GR_aggregate__exo_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

# IDP.geniUser <- Drd
idp_gUser drd
# IDP.geniUser <- Frank
idp_gUser frank
# IDP.geniUser <- Dan
idp_gUser dan
# IDP.geniUser <- Lisa
idp_gUser lisa
# IDP.geniUser <- Cary
idp_gUser cary
# IDP.geniUser <- Jim
idp_gUser jim
# IDP.geniUser <- Nancy
idp_gUser nancy
# IDP.geniUser <- Alice
idp_gUser alice 

# IDP.geniPI <- Drd
attr="[keyid:$idp].role:geniPI <- [keyid:$drd]"
name=${sidx}_IDP_geniPI__drd_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## drd requesting a sliceA from sa
makeSlice $drd sliceA Drd
## drd requesting a sliceB from sa 
makeSlice $drd sliceB Drd

## Drd gives controls permission on sliceA to Frank
attr="[keyid:$drd].role:controls([string:'sliceA']) <- [keyid:$frank]"
name=${sidx}_Drd_controls_sliceA__frank_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives controls_ permission on sliceA to Dan
attr="[keyid:$drd].role:controls_([string:'sliceA']) <- [keyid:$dan]"
name=${sidx}_Drd_controlsDel_sliceA__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Drd gives a delegate_controls_ permission on sliceA to Jim
attr="[keyid:$drd].role:delegate_controls_([string:'sliceA']) <- [keyid:$jim]"
name=${sidx}_Drd_delcontrolsDel_sliceA__jim_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## Jim gives a controls_ permission on sliceA to Nancy
attr="[keyid:$jim].role:controls_([string:'sliceA']) <- [keyid:$nancy]"
name=${sidx}_Jim_controlsDel_sliceA__nancy_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives controls permission on sliceA to Lisa 
attr="[keyid:$sa].role:controls([string:'sliceA']) <- [keyid:$lisa]"
name=${sidx}_SA_controls_sliceA__lisa_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives controls_ permission on sliceA to Dan
attr="[keyid:$sa].role:controls_([string:'sliceB']) <- [keyid:$dan]"
name=${sidx}_SA_controlsDel_sliceB__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives a delegate_controls_ permission on sliceA to Cary
attr="[keyid:$sa].role:delegate_controls_([string:'sliceA']) <- [keyid:$cary]"
name=${sidx}_SA_delcontrolsDel_sliceA__cary_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives controls_ permission on sliceA to Dan
attr="[keyid:$sa].role:controls_([string:'sliceA']) <- [keyid:$dan]"
name=${sidx}_SA_controlsDel_sliceA__dan_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives controls_ permission on sliceB to Alice
attr="[keyid:$sa].role:controls_([string:'sliceB']) <- [keyid:$alice]"
name=${sidx}_SA_controlsDel_sliceB__alice_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace

## SA gives controls_ permission on sliceA to Alice
attr="[keyid:$sa].role:controls_([string:'sliceA']) <- [keyid:$alice]"
name=${sidx}_SA_controlsDel_sliceA__alice_attr.der
creddy --attrnow --string "$attr" --out $name --out_prolog $pname --namespace $namespace



