#!/bin/sh

#SAMLDB=$p://$SERVER
SAMLDB=/var/lib/samba/private/sam.ldb

ldbsearch -H $SAMLDB --basedn='' -s base DUMMY=x dnsHostName highestCommittedUSN
if [ "$?" -ne 0 ]; then
	echo "RootDSE : ERREUR"
else
	echo "RootDSE : OK"
fi

ldbsearch -H $SAMLDB --basedn='' -s base DUMMY=x defaultNamingContext | grep defaultNamingContext >/tmp/defaultNamingContext
if [ "$?" -ne 0 ]; then
	echo "Getting defaultNamingContext  : ERREUR"
fi

BASEDN=`awk '{print $2}' </tmp/defaultNamingContext`
echo "Getting defaultNamingContext is $BASEDN"

ldbsearch -H $SAMLDB '(objectclass=user)' sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Listing Users : ERREUR"
fi

ldbsearch -H $SAMLDB -S '(objectclass=user)' sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Listing Users (sorted) : ERREUR"
fi

ldbsearch -H $SAMLDB '(objectclass=group)' sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Listing Groups : ERREUR"
fi

ldbsearch -H $SAMLDB '(|(|(&(!(groupType:1.2.840.113556.1.4.803:=1))(groupType:1.2.840.113556.1.4.803:=2147483648)(groupType:1.2.840.113556.1.4.804:=10))(samAccountType=805306368))(samAccountType=805306369))' sAMAccountName | grep sAMAccountName >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "sAMAccountName : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
echo "Found $nentries entries"
if [ "$nentries" -lt 10 ]; then
	echo "Should have found at least 10 entries : ERREUR"
else
	echo "Should have found at least 10 entries : OK"
fi

ldbsearch -H $SAMLDB -s base -b "" '(objectclass=*)' | grep -i supportedControl >/tmp/supportedControl
if [ "$?" -ne 0 ]; then
	echo "Check rootDSE for Controls : ERREUR"
fi

nentries=`wc -l </tmp/supportedControl`
if [ "$nentries" -lt 4 ]; then
	echo "Should have found at least 4 entries : ERREUR"
else
	echo "Should have found at least 4 entries : OK"
fi

echo "Test Paged Results Control"
ldbsearch -H $SAMLDB --controls=paged_results:1:5 '(objectclass=user)' | grep sAMAccountName >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Paged Results Control : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
if [ "$nentries" -lt 1 ]; then
	echo "Paged Results Control test returned 0 items : ERREUR"
else
	echo "Paged Results Control test returned 0 items : OK"
fi

ldbsearch -H $SAMLDB --controls=server_sort:1:0:sAMAccountName '(objectclass=user)' | grep sAMAccountName >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Server Sort Control : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
if [ "$nentries" -lt 1 ]; then
	echo "Server Sort Control test returned 0 items : ERREUR"
else
	echo "Server Sort Control test returned 0 items : OK"
fi

echo "Test Extended DN Control"
ldbsearch -H $SAMLDB --controls=extended_dn:1:0 '(objectclass=user)' | grep sAMAccountName  >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Extended DN Control : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
if [ "$nentries" -lt 1 ]; then
	echo "Extended DN Control test returned 0 items : ERREUR"
else
	echo "Extended DN Contro : OK"
fi

ldbsearch -H $SAMLDB --controls=domain_scope:1 '(objectclass=user)' | grep sAMAccountName >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Extended Domain scope Control : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
if [ "$nentries" -lt 1 ]; then
	echo "Extended Domain scope Control test returned 0 items : ERREUR"
else
	echo "Extended Domain scope Control : OK"
fi

ldbsearch -H $SAMLDB --controls=asq:1:member -s base -b "CN=Administrators,CN=Builtin,$BASEDN" | grep sAMAccountName >/tmp/sAMAccountName
if [ "$?" -ne 0 ]; then
	echo "Attribute Scope Query Control : ERREUR"
fi

nentries=`wc -l </tmp/sAMAccountName`
if [ "$nentries" -lt 1 ]; then
	echo "Attribute Scope Query test returned 0 items : ERREUR"
else
	echo "Attribute Scope Query Control : OK"
fi

ldbsearch -H $SAMLDB --controls=search_options:1:2 '(objectclass=crossRef)' | grep crossRef >/tmp/crossRef
if [ "$?" -ne 0 ]; then
	echo "Search Options Control : ERREUR"
fi

nentries=`wc -l </tmp/crossRef`
if [ "$nentries" -lt 1 ]; then
	echo "Search Options Control Query test returned 0 items : ERREUR"
else
	echo "Search Options Control : OK"
fi

