1#! /bin/sh
2# $OpenLDAP$
3## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4##
5## Copyright 1998-2021 The OpenLDAP Foundation.
6## Portions Copyright 2008 Red Hat, Inc. 
7## All rights reserved.
8##
9## Redistribution and use in source and binary forms, with or without
10## modification, are permitted only as authorized by the OpenLDAP
11## Public License.
12##
13## A copy of this license is available in the file LICENSE in the
14## top-level directory of the distribution or, alternatively, at
15## <http://www.OpenLDAP.org/license.html>.
16
17echo "running defines.sh"
18. $SRCDIR/scripts/defines.sh
19
20if test $MEMBEROF = memberofno; then 
21	echo "Memberof overlay not available, test skipped"
22	exit 0
23fi 
24
25if test $REFINT = refintno; then 
26	echo "Referential Integrity overlay not available, test skipped"
27	exit 0
28fi 
29
30mkdir -p $TESTDIR $DBDIR1 $TESTDIR/confdir
31
32$SLAPPASSWD -g -n >$CONFIGPWF
33echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >$TESTDIR/configpw.conf
34
35echo "Starting slapd on TCP/IP port $PORT1..."
36. $CONFFILTER $BACKEND < $NAKEDCONF > $CONF1
37$SLAPD -f $CONF1 -F $TESTDIR/confdir -h $URI1 -d $LVL > $LOG1 2>&1 &
38PID=$!
39if test $WAIT != 0 ; then
40    echo PID $PID
41    read foo
42fi
43KILLPIDS="$PID"
44
45sleep 1
46for i in 0 1 2 3 4 5; do
47	$LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
48		'objectclass=*' > /dev/null 2>&1
49	RC=$?
50	if test $RC = 0 ; then
51		break
52	fi
53	echo "Waiting 5 seconds for slapd to start..."
54	sleep 5
55done
56if test $RC != 0 ; then
57	echo "ldapsearch failed ($RC)!"
58	test $KILLSERVERS != no && kill -HUP $KILLPIDS
59	exit $RC
60fi
61
62cat /dev/null > $TESTOUT
63
64indexInclude="" mainInclude="" nullExclude=""
65test $INDEXDB = indexdb	|| indexInclude="# "
66test $MAINDB  = maindb	|| mainInclude="# "
67case $BACKEND in
68null) nullExclude="# " ;;
69esac
70
71if [ "$MEMBEROF" = memberofmod ]; then
72	echo "Inserting memberof overlay on provider..."
73	$LDAPADD -D cn=config -H $URI1 -y $CONFIGPWF <<EOF > $TESTOUT 2>&1
74dn: cn=module,cn=config
75objectClass: olcModuleList
76cn: module
77olcModulePath: ../servers/slapd/overlays
78olcModuleLoad: memberof.la
79olcModuleLoad: refint.la
80EOF
81	RC=$?
82	if test $RC != 0 ; then
83		echo "ldapadd failed for moduleLoad ($RC)!"
84		test $KILLSERVERS != no && kill -HUP $KILLPIDS
85		exit $RC
86	fi
87fi
88
89echo "Running ldapadd to build slapd config database..."
90$LDAPADD -H $URI1 -D 'cn=config' -w `cat $CONFIGPWF` \
91	>> $TESTOUT 2>&1 <<EOF
92dn: olcDatabase={1}$BACKEND,cn=config
93objectClass: olcDatabaseConfig
94${nullExclude}objectClass: olc${BACKEND}Config
95olcDatabase: {1}$BACKEND
96olcSuffix: $BASEDN
97olcRootDN: cn=Manager,$BASEDN
98olcRootPW:: c2VjcmV0
99olcMonitoring: TRUE
100${nullExclude}olcDbDirectory: $TESTDIR/db.1.a/
101${indexInclude}olcDbIndex: objectClass eq
102${indexInclude}olcDbIndex: cn pres,eq,sub
103${indexInclude}olcDbIndex: uid pres,eq,sub
104${indexInclude}olcDbIndex: sn pres,eq,sub
105${mainInclude}olcDbMode: 384
106
107# {0}memberof, {1}$BACKEND, config
108dn: olcOverlay={0}memberof,olcDatabase={1}$BACKEND,cn=config
109objectClass: olcOverlayConfig
110objectClass: olcMemberOfConfig
111olcOverlay: {0}memberof
112olcMemberOfRefInt: TRUE
113olcMemberOfGroupOC: groupOfNames
114olcMemberOfMemberAD: member
115olcMemberOfMemberOfAD: memberOf
116
117# {1}refint, {1}$BACKEND, config
118dn: olcOverlay={1}refint,olcDatabase={1}$BACKEND,cn=config
119objectClass: olcOverlayConfig
120objectClass: olcRefintConfig
121olcOverlay: {1}refint
122olcRefintAttribute: member
123olcRefintAttribute: memberOf
124EOF
125RC=$?
126if test $RC != 0 ; then
127	echo "ldapadd failed ($RC)!"
128	test $KILLSERVERS != no && kill -HUP $KILLPIDS
129	exit $RC
130fi
131
132echo "Running ldapadd to build slapd database..."
133$LDAPADD -H $URI1 \
134	-D "cn=Manager,$BASEDN" -w secret \
135	>> $TESTOUT 2>&1 << EOF
136dn: $BASEDN
137objectClass: organization
138objectClass: dcObject
139o: Example, Inc.
140dc: example
141
142dn: ou=People,$BASEDN
143objectClass: organizationalUnit
144ou: People
145
146dn: ou=Groups,$BASEDN
147objectClass: organizationalUnit
148ou: Groups
149
150dn: cn=Roger Rabbit,ou=People,$BASEDN
151objectClass: inetOrgPerson
152cn: Roger Rabbit
153sn: Rabbit
154
155dn: cn=Baby Herman,ou=People,$BASEDN
156objectClass: inetOrgPerson
157cn: Baby Herman
158sn: Herman
159
160dn: cn=Cartoonia,ou=Groups,$BASEDN
161objectClass: groupOfNames
162cn: Cartoonia
163member: cn=Roger Rabbit,ou=People,$BASEDN
164member: cn=Baby Herman,ou=People,$BASEDN
165EOF
166RC=$?
167if test $RC != 0 ; then
168	echo "ldapadd failed ($RC)!"
169	test $KILLSERVERS != no && kill -HUP $KILLPIDS
170	exit $RC
171fi
172
173echo "Search the entire database..."
174echo "# Search the entire database..." >> $SEARCHOUT
175$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
176	'(objectClass=*)' '*' memberOf >> $SEARCHOUT 2>&1
177RC=$?
178if test $RC != 0 ; then
179	echo "ldapsearch failed ($RC)!"
180	test $KILLSERVERS != no && kill -HUP $KILLPIDS
181	exit $RC
182fi
183
184echo "Running ldapmodify to rename subtree..."
185$LDAPMODIFY -H $URI1 \
186	-D "cn=Manager,$BASEDN" -w secret \
187	>> $TESTOUT 2>&1 << EOF
188dn: ou=People,$BASEDN
189changetype: modrdn
190newrdn: ou=Toons
191deleteoldrdn:1
192newsuperior: $BASEDN
193EOF
194
195# refint runs in a background thread, so it most likely won't complete
196# before the modify returns. Give it some time to execute.
197sleep $SLEEP0
198
199echo "Re-search the entire database..."
200echo "# Re-search the entire database..." >> $SEARCHOUT
201$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
202	'(objectClass=*)' '*' memberOf >> $SEARCHOUT 2>&1
203RC=$?
204if test $RC != 0 ; then
205	echo "ldapsearch failed ($RC)!"
206	test $KILLSERVERS != no && kill -HUP $KILLPIDS
207	exit $RC
208fi
209
210echo "Running ldapmodify to rename subtree..."
211$LDAPMODIFY -H $URI1 \
212	-D "cn=Manager,$BASEDN" -w secret \
213	>> $TESTOUT 2>&1 << EOF
214dn: ou=Groups,$BASEDN
215changetype: modrdn
216newrdn: ou=Studios
217deleteoldrdn:1
218newsuperior: $BASEDN
219EOF
220
221sleep $SLEEP0
222
223echo "Re-search the entire database..."
224echo "# Re-search the entire database..." >> $SEARCHOUT
225$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
226	'(objectClass=*)' '*' memberOf >> $SEARCHOUT 2>&1
227RC=$?
228if test $RC != 0 ; then
229	echo "ldapsearch failed ($RC)!"
230	test $KILLSERVERS != no && kill -HUP $KILLPIDS
231	exit $RC
232fi
233
234echo "Running ldapdelete to remove a member..."
235$LDAPMODIFY -H $URI1 \
236	-D "cn=Manager,$BASEDN" -w secret \
237	>> $TESTOUT 2>&1 << EOF
238dn: cn=Baby Herman,ou=Toons,$BASEDN
239changetype: delete
240EOF
241
242sleep $SLEEP0
243
244echo "Re-search the entire database..."
245echo "# Re-search the entire database..." >> $SEARCHOUT
246$LDAPSEARCH -S "" -b "$BASEDN" -H $URI1 \
247	'(objectClass=*)' '*' memberOf >> $SEARCHOUT 2>&1
248RC=$?
249if test $RC != 0 ; then
250	echo "ldapsearch failed ($RC)!"
251	test $KILLSERVERS != no && kill -HUP $KILLPIDS
252	exit $RC
253fi
254
255test $KILLSERVERS != no && kill -HUP $KILLPIDS
256
257LDIF=$MEMBEROFREFINTOUT
258
259echo "Filtering ldapsearch results..."
260$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
261echo "Filtering original ldif used to create database..."
262$LDIFFILTER < $LDIF > $LDIFFLT
263echo "Comparing filter output..."
264$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
265
266if test $? != 0 ; then
267	echo "Comparison failed"
268	exit 1
269fi
270
271echo ">>>>> Test succeeded"
272
273test $KILLSERVERS != no && wait
274
275exit 0
276