#!/bin/sh
#
#
TestName="allowed-jobcommand-test"
JobName=BackupClient1
. scripts/functions

scripts/cleanup
scripts/copy-test-confs


# create the run before job script
echo "#!/bin/sh
echo allowed job command: runbeforejob
" > $working/runbeforejob.sh
chmod +x $working/runbeforejob.sh

rm -f bin/bareos-dir.conf
/bin/cp -f ${rconfigs}/${TestName}/bareos-dir.conf.allowed-jobcommand-test bin/bareos-dir.conf
/bin/cp -f ${rconfigs}/${TestName}/bareos-fd.conf.allowed-jobcommand-test bin/bareos-fd.conf
/bin/cp -f ${rconfigs}/${TestName}/bareos-fd.conf.allowed-jobcommand-test bin/bareos-fd.conf.orig

# create test data
dd if=/dev/zero of=${cwd}/tmp/testdata bs=400k count=1

echo "${cwd}/tmp/testdata" >${cwd}/tmp/file-list

cp ${cwd}/tmp/file-list ${cwd}/tmp/restore-list


start_test

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
@$out ${cwd}/tmp/backup.log
label storage=File volume=TestVolume001
run job=$JobName Level=Full yes
wait
messages

@$out ${cwd}/tmp/estimate.log
estimate job=$JobName yes
wait
messages

@$out ${cwd}/tmp/restore.log
restore where=${cwd}/tmp/bareos-restores storage=File
7
<${cwd}/tmp/restore-list

yes
wait
messages

END_OF_DATA

run_bareos
#check_for_zombie_jobs storage=File
stop_bareos


grep "Termination:            Backup OK" ${tmp}/backup.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Backup was OK"
else
   estat=1
fi

grep "2000 OK estimate files=1 bytes=" ${tmp}/estimate.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "estimate OK"
else
   estat=2
fi

grep "Termination:            Restore OK" ${tmp}/restore.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Restore OK"
else
   estat=3
fi





# remove the runscript command from allowed commands
sed 's@Allowed Job Command = runscript@# Allowed Job Command = runscript@g' >bin/bareos-fd.conf <bin/bareos-fd.conf.orig

# retry to run  backup, estimate and restore

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
@$out ${cwd}/tmp/backup2.log
run job=$JobName Level=Full yes
wait
messages

@$out ${cwd}/tmp/estimate2.log
estimate job=$JobName yes
wait
messages

@$out ${cwd}/tmp/restore2.log
restore where=${cwd}/tmp/bareos-restores storage=File
7
<${cwd}/tmp/restore-list

yes
wait
messages

END_OF_DATA

run_bareos
#check_for_zombie_jobs storage=File
stop_bareos


grep "Fatal error: Illegal runscript command not allowed " ${tmp}/backup2.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "runscript was blocked : OK"
else
   estat=4
fi

grep "2000 OK estimate files=1 bytes=" ${tmp}/estimate2.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "estimate OK"
else
   estat=5
fi

grep "Termination:            Restore OK" ${tmp}/restore2.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Restore OK"
else
   estat=6
fi




# remove the estimate command from allowed commands
sed 's@Allowed Job Command = estimate@# Allowed Job Command = estimate@g' >bin/bareos-fd.conf <bin/bareos-fd.conf.orig

# retry to run  backup, estimate and restore

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
@$out ${cwd}/tmp/backup3.log
run job=$JobName Level=Full yes
wait
messages

@$out ${cwd}/tmp/estimate3.log
estimate job=$JobName yes
wait
messages

@$out ${cwd}/tmp/restore3.log
restore where=${cwd}/tmp/bareos-restores storage=File
7
<${cwd}/tmp/restore-list

yes
wait
messages

END_OF_DATA

run_bareos
#check_for_zombie_jobs storage=File
stop_bareos

grep "Termination:            Backup OK" ${tmp}/backup3.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Backup was OK"
else
   estat=7
fi


grep "Illegal estimate command not allowed by Allowed Job Cmds setting of this filed." ${tmp}/estimate3.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "estimate blocked : OK"
else
   estat=8
fi

grep "Termination:            Restore OK" ${tmp}/restore3.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Restore OK"
else
   estat=9
fi







# remove the restore command from allowed commands
sed 's@Allowed Job Command = restore@# Allowed Job Command = restore@g' >bin/bareos-fd.conf <bin/bareos-fd.conf.orig

# retry to run  backup, estimate and restore

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
messages
@$out ${cwd}/tmp/backup4.log
run job=$JobName Level=Full yes
wait
messages

@$out ${cwd}/tmp/estimate4.log
estimate job=$JobName yes
wait
messages

@$out ${cwd}/tmp/restore4.log
restore where=${cwd}/tmp/bareos-restores storage=File
7
<${cwd}/tmp/restore-list

yes
wait
messages

END_OF_DATA

run_bareos
#check_for_zombie_jobs storage=File
stop_bareos

grep "Termination:            Backup OK" ${tmp}/backup4.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Backup was OK"
else
   estat=10
fi

grep "2000 OK estimate files=1 bytes=" ${tmp}/estimate4.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "estimate OK"
else
   estat=11
fi

grep "Fatal error: Illegal \"restore\" command not allowed by Allowed Job Cmds setting of this filed." ${tmp}/restore4.log 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "Restore Blocked: OK"
else
   estat=12
fi

end_test
