#!/bin/sh
#
# Copyright (c) 2010 Kungliga Tekniska Högskolan
# (Royal Institute of Technology, Stockholm, Sweden). 
# All rights reserved. 
#
# Portions Copyright (c) 2010 Apple Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met: 
#
# 1. Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer. 
#
# 2. Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#
# 3. Neither the name of the Institute nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
#
# THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
# SUCH DAMAGE. 

env_setup="@env_setup@"

. ${env_setup}

confdir="@confdir@"
testdir="@testdir@"

echo "dumping in different formats"

conf="${confdir}/krb5-dump.conf"
realm=LKDC:SHA1.58FCE36DBDBD86AE701093B4B1D79ADD0705D00B
realm2=LKDC:SHA1.doesnotexists

sf="cifs%LKDC:SHA1.58FCE36DBDBD86AE701093B4B1D79ADD0705D00B@LKDC:SHA1.58FCE36DBDBD86AE701093B4B1D79ADD0705D00B.plist"

test -d "${testdir}" || mkdir "${testdir}"
cd "${testdir}"

rm -f file *.plist

echo "dump"
${kadmin} -r ${realm} -c ${conf} -l dump -d file || exit 1

grep cifs/LKDC file > /dev/null || exit 1

rm -f file

echo "dump format=od"
${kadmin} -r ${realm} -c ${conf} -l dump -d --format=od . || exit 1

found=0
for a in *.plist; do
	if /usr/libexec/PlistBuddy -c 'print KerberosPrincipal' $a 2>&1 | grep 'cifs/LKDC' > /dev/null 2>&1 ; then
		found=1
	fi
done
if test $found != 1; then
	echo "cifs@LKDC not found"
	exit 1
fi

rm -f *.plist

echo "mit-dump-file"
${kadmin} -r ${realm} -c ${conf} -l dump -d --mit-dump-file="${confdir}/dumpfile" file || exit 1

grep cifs/LKDC file > /dev/null || exit 1

rm -rf file

echo "mit-dump-file format=od"
${kadmin} -r ${realm} -c ${conf} -l dump -d --mit-dump-file="${confdir}/dumpfile" --format=od . || exit 1

found=0
for a in *.plist; do
	if /usr/libexec/PlistBuddy -c 'print KerberosPrincipal' $a 2>&1 | grep 'cifs/LKDC' > /dev/null 2>&1 ; then
		found=1
	fi
done
if test $found != 1; then
	echo "cifs@LKDC not found"
	exit 1
fi

rm -f *.plist file

echo "check for database that doesn't exists"
${kadmin} -r ${realm2} -c ${conf} -l dump file > /dev/null 2>/dev/null && exit 1

exit 0
