#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: "Upload printer driver files with different case"
## exposure: dangerous
## packages:
##  - univention-samba | univention-samba4
## bugs: [52051, 55940]
## roles:
## - domaincontroller_master
## - domaincontroller_backup
## - domaincontroller_slave
## - memberserver

# shellcheck disable=SC1091
. "$TESTLIBPATH/base.sh" || exit 137
# shellcheck disable=SC2154
admin_account="$(echo "$tests_domainadmin_account" | sed -e 's|uid=||;s|,.*||')"
# shellcheck disable=SC2154
admin_passwd="$tests_domainadmin_pwd"

declare -a files_to_remove
declare -a drivers_to_remove
driver_dir="/var/lib/samba/drivers/x64"

cleanup () {
	for file in "${files_to_remove[@]}"; do
		rm -f "$driver_dir/3/$file"
		rm -f "$driver_dir/$file"
	done
	for driver in "${drivers_to_remove[@]}"; do
		rpcclient -U "$admin_account%$admin_passwd" localhost -c "deldriverex $driver"
	done
}
trap cleanup INT TERM EXIT

upload_driver_file () {
	local tmp_file
	tmp_file="$(su "$admin_account" -c "mktemp")"
	cd /tmp || fail_fast 1 "cd /tmp failed"
	tmp_file="$(basename "$tmp_file")"
	smbclient "//$(hostname)/print\$" -U "$admin_account%$admin_passwd" -c "prompt; put $tmp_file x64/$tmp_file" || fail_fast 1 "smbclient failed"
	echo "$tmp_file"
}

upload_printer_driver () {
	local driver_file="$1" driver_name data_file config_file help_file
	driver_name="test-driver-$(makepasswd)"
	data_file="$(upload_driver_file)"
	config_file="$(upload_driver_file)"
	help_file="$(upload_driver_file)"
	local driver_settings="$driver_name"
	driver_settings+=":$(basename "$driver_file")"
	driver_settings+=":$(basename "$data_file")"
	driver_settings+=":$(basename "$config_file")"
	driver_settings+=":$(basename "$help_file")"
	driver_settings+=":"
	driver_settings+=":RAW:"
	files_to_remove+=("$driver_dir/$driver_file")
	files_to_remove+=("$data_file")
	files_to_remove+=("$config_file")
	files_to_remove+=("$help_file")
	rpcclient \
		-U "$admin_account%$admin_passwd" localhost \
		-c "adddriver 'Windows x64' '$driver_settings'" || fail_fast 1 "adddriver failed"
	drivers_to_remove+=("$driver_name")
}

check_driver_file () {
	local driver_file="$1" md5sum="$2" md5sum_new
	md5sum_new="$(md5sum "$driver_dir/3/$driver_file" | awk '{print $1}')"
	if [ "$md5sum_new" = "$md5sum" ]; then
		return 0
	else
		return 1
	fi
}

rm -f "$driver_dir/MOIN.DLL"
rm -f "$driver_dir/3/MOIN.DLL"
rm -f "$driver_dir/moin.dll"
rm -f "$driver_dir/3/moin.dll"

# this should work
echo "version1" > "$driver_dir/MOIN.DLL"
chown "$admin_account" "$driver_dir/MOIN.DLL"
md5sum_1="$(md5sum "$driver_dir/MOIN.DLL" | awk '{print $1}')"
upload_printer_driver "MOIN.DLL"
check_driver_file MOIN.DLL "$md5sum_1" || fail_fast 1 "driver file checksum mismatch in download area"
rm -f "$driver_dir/MOIN.DLL"

# check that this file is not uploaded
echo "version2" > "$driver_dir/moin.dll"
chown "$admin_account" "$driver_dir/moin.dll"
touch -a -m -d "-20days" "$driver_dir/moin.dll"
upload_printer_driver "moin.dll"
check_driver_file MOIN.DLL "$md5sum_1" || fail_fast 1 "driver file checksum mismatch in download area, driver overwritten?"
test -e "$driver_dir/3/moin.dll" && fail_fast 1 "driver file exist but shouldn't"
rm -f "$driver_dir/moin.dll"

# check that this file is upload (modify time is older than the first file)
# and MOIN.DLL is overwritten
echo "version3" > "$driver_dir/moin.dll"
chown "$admin_account" "$driver_dir/moin.dll"
touch -a -m -d "+10days" "$driver_dir/moin.dll"
md5sum_3="$(md5sum "$driver_dir/moin.dll" | awk '{print $1}')"
upload_printer_driver "moin.dll"
check_driver_file MOIN.DLL "$md5sum_3" || fail_fast 1 "driver not overwritten, but should be"
! test -e "$driver_dir/3/moin.dll" || fail_fast 1 "driver file exists, but shouldn't"
rm -f "$driver_dir/moin.dll"

# and again
echo "version4" > "$driver_dir/moin.DLL"
chown "$admin_account" "$driver_dir/moin.DLL"
touch -a -m -d "+20days" "$driver_dir/moin.DLL"
md5sum_4="$(md5sum "$driver_dir/moin.DLL" | awk '{print $1}')"
upload_printer_driver "moin.DLL"
check_driver_file MOIN.DLL "$md5sum_4" || fail_fast 1 "driver not overwritten, but should be"
! test -e "$driver_dir/3/moin.DLL" || fail_fast 1 "driver file exists, but shouldn't"
! test -e "$driver_dir/3/moin.dll" || fail_fast 1 "driver file exists, but shouldn't"
rm -f "$driver_dir/moin.DLL"

exit "$RETVAL"
