#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Use incorrect name/password to connect each daemon
# and check that we are properly kicked out
#
TestName="bad-password-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
setdebug level=100 trace=1 client
setdebug level=100 trace=1 storage
quit
END_OF_DATA

run_bacula

cat <<END_OF_DATA >$tmp/bconcmds
@output /dev/null
messages
@$out $tmp/log1.out
reload
status dir
@$out $tmp/log2.out
status client
@$out $tmp/log3.out
status storage
quit
END_OF_DATA

run_bconsole

cp $conf/*.conf $tmp/
cp $tmp/bconcmds $tmp/bconcmds.tpl

################################################################
# Test FD with incorrect password
cp $tmp/*.conf $conf/
sed s:/log:/log-client: $tmp/bconcmds.tpl > $tmp/bconcmds

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Password', 'xxx', 'Client')"
run_bconsole

################################################################
# Test SD with incorrect password
cp $tmp/*.conf $conf/
sed s:/log:/log-sd: $tmp/bconcmds.tpl > $tmp/bconcmds

$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Password', 'xxx', 'Storage')"
$bperl -e "add_attribute('$conf/bacula-dir.conf', 'Password', 'xxx', 'Autochanger')"
run_bconsole

################################################################
# Test DIR with incorrect password

cp $tmp/*.conf $conf/
sed s:/log:/log-cons: $tmp/bconcmds.tpl > $tmp/bconcmds

$bperl -e "add_attribute('$conf/bconsole.conf', 'Password', 'xxx', 'Director')"
run_bconsole

################################################################
# Test back with good passwords
cp $tmp/*.conf $conf/
sed s:/log:/log-back: $tmp/bconcmds.tpl > $tmp/bconcmds

run_bconsole

################################################################
# Check logs, all daemons should answer the call
nb=`grep "Daemon started" $tmp/log-back* | wc -l`
if [ $nb != 3 ]; then
    print_debug "ERROR: Should find 3 daemon status in $tmp/log-back*"
    estat=1
fi

# Threads are taking some time to cleanup the memory
sleep 7

export CHECK_JOBMEDIA=0
export CHECK_ENCRYPTION=0

stop_bacula
end_test
