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## All rights reserved.
7##
8## Redistribution and use in source and binary forms, with or without
9## modification, are permitted only as authorized by the OpenLDAP
10## Public License.
11##
12## A copy of this license is available in the file LICENSE in the
13## top-level directory of the distribution or, alternatively, at
14## <http://www.OpenLDAP.org/license.html>.
15
16echo "running defines.sh"
17. $SRCDIR/scripts/defines.sh
18
19if test $BACKLDAP = "ldapno" ; then 
20	echo "LDAP backend not available, test skipped"
21	exit 0
22fi 
23
24rm -rf $TESTDIR
25
26mkdir -p $TESTDIR $DBDIR1 $DBDIR2
27
28echo "Running slapadd to build slapd database..."
29. $CONFFILTER $BACKEND < $CHAINCONF1 > $ADDCONF
30. $CONFFILTER < $LDIFCHAIN1 > $SEARCHOUT
31$SLAPADD -f $ADDCONF -l $SEARCHOUT
32RC=$?
33if test $RC != 0 ; then
34	echo "slapadd 1 failed ($RC)!"
35	exit $RC
36fi
37
38. $CONFFILTER $BACKEND < $CHAINCONF2 > $ADDCONF
39. $CONFFILTER < $LDIFCHAIN2 > $SEARCHOUT
40$SLAPADD -f $ADDCONF -l $SEARCHOUT
41RC=$?
42if test $RC != 0 ; then
43	echo "slapadd 2 failed ($RC)!"
44	exit $RC
45fi
46
47echo "Starting first slapd on TCP/IP port $PORT1..."
48. $CONFFILTER $BACKEND < $CHAINCONF1 > $CONF1
49$SLAPD -f $CONF1 -h $URI1 -d $LVL > $LOG1 2>&1 &
50PID1=$!
51if test $WAIT != 0 ; then
52    echo PID $PID1
53    read foo
54fi
55KILLPIDS="$PID1"
56
57echo "Starting second slapd on TCP/IP port $PORT2..."
58. $CONFFILTER $BACKEND < $CHAINCONF2 > $CONF2
59$SLAPD -f $CONF2 -h $URI2 -d $LVL > $LOG2 2>&1 &
60PID2=$!
61if test $WAIT != 0 ; then
62    echo PID $PID2
63    read foo
64fi
65
66KILLPIDS="$KILLPIDS $PID2"
67
68sleep 1
69
70echo "Using ldapsearch to check that first slapd is running..."
71for i in 0 1 2 3 4 5; do
72	$LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
73		'objectclass=*' > /dev/null 2>&1
74	RC=$?
75	if test $RC = 0 ; then
76		break
77	fi
78	echo "Waiting 5 seconds for slapd to start..."
79	sleep 5
80done
81
82if test $RC != 0 ; then
83	echo "ldapsearch failed ($RC)!"
84	test $KILLSERVERS != no && kill -HUP $KILLPIDS
85	exit $RC
86fi
87
88echo "Using ldapsearch to check that second slapd is running..."
89for i in 0 1 2 3 4 5; do
90	$LDAPSEARCH -s base -b "$MONITOR" -H $URI2 \
91		'objectclass=*' > /dev/null 2>&1
92	RC=$?
93	if test $RC = 0 ; then
94		break
95	fi
96	echo "Waiting 5 seconds for slapd to start..."
97	sleep 5
98done
99
100if test $RC != 0 ; then
101	echo "ldapsearch failed ($RC)!"
102	test $KILLSERVERS != no && kill -HUP $KILLPIDS
103	exit $RC
104fi
105
106for n in 1 2 ; do
107	URI=`eval echo '$URI'$n`
108	echo "Testing ldapsearch as anonymous for \"$BASEDN\" on server $n..."
109	$LDAPSEARCH -H $URI -b "$BASEDN" -S "" \
110		 > $SEARCHOUT 2>&1
111
112	RC=$?
113	if test $RC != 0 ; then
114		echo "ldapsearch failed ($RC)!"
115		test $KILLSERVERS != no && kill -HUP $KILLPIDS
116		exit $RC
117	fi
118
119	echo "Filtering ldapsearch results..."
120	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
121	echo "Filtering original ldif used to create database..."
122	$LDIFFILTER < $CHAINOUT > $LDIFFLT
123	echo "Comparing filter output..."
124	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
125	
126	if test $? != 0 ; then
127		echo "comparison failed - chained search didn't succeed"
128		test $KILLSERVERS != no && kill -HUP $KILLPIDS
129		exit 1
130	fi
131
132	echo "Reading the referral entry \"ou=Other,$BASEDN\" as anonymous on server $n..."
133	$LDAPSEARCH -H $URI -b "ou=Other,$BASEDN" -S "" \
134		 > $SEARCHOUT 2>&1
135
136	RC=$?
137	if test $RC != 0 ; then
138		echo "ldapsearch failed ($RC)!"
139		test $KILLSERVERS != no && kill -HUP $KILLPIDS
140		exit $RC
141	fi
142
143	echo "Filtering ldapsearch results..."
144	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
145	echo "Filtering original ldif used to create database..."
146	$LDIFFILTER < $CHAINREFOUT > $LDIFFLT
147	echo "Comparing filter output..."
148	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
149		
150	if test $? != 0 ; then
151		echo "comparison failed - chained search didn't succeed"
152		test $KILLSERVERS != no && kill -HUP $KILLPIDS
153		exit 1
154	fi
155
156	DN="cn=Mark Elliot,ou=Alumni Association,ou=People,$BASEDN"
157	echo "Comparing \"$DN\" on server $n..."
158	$LDAPCOMPARE -H $URI "$DN" "cn:Mark Elliot" \
159		 > $TESTOUT 2>&1
160
161	RC=$?
162	if test $RC != 6 && test $RC,$BACKEND != 5,null ; then
163		echo "ldapcompare failed ($RC)!"
164		test $KILLSERVERS != no && kill -HUP $KILLPIDS
165		exit 1
166	fi
167
168	DN="ou=Other,$BASEDN"
169	echo "Comparing \"$DN\" on server $n with manageDSAit control..."
170	$LDAPCOMPARE -H $URI -M "$DN" "ou:Other" \
171		 > $TESTOUT 2>&1
172
173	RC=$?
174	if test $RC != 6 && test $RC,$BACKEND != 5,null ; then
175		echo "ldapcompare failed ($RC)!"
176		test $KILLSERVERS != no && kill -HUP $KILLPIDS
177		exit 1
178	fi
179done
180
181#
182# Testing writes to first server
183#
184echo "Writing to first server with scope on second server..."
185$LDAPMODIFY -v -D "$MANAGERDN" -H $URI1 -w $PASSWD > \
186	$TESTOUT 2>&1 << EOMODS
187dn: cn=New Group,ou=Groups,dc=example,dc=com
188changetype: add
189objectClass: groupOfNames
190cn: New Group
191member:
192
193dn: cn=New Group,ou=Groups,dc=example,dc=com
194changetype: modify
195add: description
196description: testing chain overlay writes...
197-
198replace: member
199member: cn=New Group,ou=Groups,dc=example,dc=com
200member: cn=Manager,dc=example,dc=com
201-
202add: owner
203owner: cn=Manager,dc=example,dc=com
204-
205
206dn: cn=New Group,ou=Groups,dc=example,dc=com
207changetype: modrdn
208newrdn: cn=Renamed Group
209deleteoldrdn: 1
210
211dn: cn=All Staff,ou=Groups,dc=example,dc=com
212changetype: delete
213EOMODS
214
215RC=$?
216if test $RC != 0 ; then
217	echo "ldapmodify failed ($RC)!"
218	test $KILLSERVERS != no && kill -HUP $KILLPIDS
219	exit $RC
220fi
221
222#
223# Testing writes to second server
224#
225echo "Writing to second server with scope on first server..."
226$LDAPMODIFY -v -D "$MANAGERDN" -H $URI2 -w $PASSWD > \
227	$TESTOUT 2>&1 << EOMODS
228dn: cn=New User,ou=People,dc=example,dc=com
229changetype: add
230objectClass: person
231cn: New User
232sn: User
233seeAlso: cn=New Group,ou=Groups,dc=example,dc=com
234
235dn: cn=New User,ou=People,dc=example,dc=com
236changetype: modify
237add: description
238description: testing chain overlay writes...
239-
240replace: seeAlso
241seeAlso: cn=Renamed Group,ou=Groups,dc=example,dc=com
242-
243
244dn: cn=New User,ou=People,dc=example,dc=com
245changetype: modrdn
246newrdn: cn=Renamed User
247deleteoldrdn: 1
248
249dn: cn=Ursula Hampster,ou=Alumni Association,ou=People,dc=example,dc=com
250changetype: delete
251EOMODS
252
253RC=$?
254if test $RC != 0 ; then
255	echo "ldapmodify failed ($RC)!"
256	test $KILLSERVERS != no && kill -HUP $KILLPIDS
257	exit $RC
258fi
259
260for n in 1 2 ; do
261	URI=`eval echo '$URI'$n`
262	echo "Testing ldapsearch as anonymous for \"$BASEDN\" on server $n..."
263	$LDAPSEARCH -H $URI -b "$BASEDN" -S "" \
264		 > $SEARCHOUT 2>&1
265
266	RC=$?
267	if test $RC != 0 ; then
268		echo "ldapsearch failed ($RC)!"
269		test $KILLSERVERS != no && kill -HUP $KILLPIDS
270		exit $RC
271	fi
272
273	echo "Filtering ldapsearch results..."
274	$LDIFFILTER < $SEARCHOUT > $SEARCHFLT
275	echo "Filtering original ldif used to create database..."
276	$LDIFFILTER < $CHAINMODOUT > $LDIFFLT
277	echo "Comparing filter output..."
278	$CMP $SEARCHFLT $LDIFFLT > $CMPOUT
279	
280	if test $? != 0 ; then
281		echo "comparison failed - chained search didn't succeed"
282		test $KILLSERVERS != no && kill -HUP $KILLPIDS
283		exit 1
284	fi
285done
286
287NEWPW=newsecret
288echo "Using ldappasswd on second server with scope on first server..."
289$LDAPPASSWD -H $URI2 \
290	-w secret -s $NEWPW \
291	-D "$MANAGERDN" "$BJORNSDN" >> $TESTOUT 2>&1
292RC=$?
293if test $RC != 0 ; then
294	echo "ldappasswd failed ($RC)!"
295	test $KILLSERVERS != no && kill -HUP $KILLPIDS
296	exit $RC
297fi
298
299echo "Binding with newly changed password on first server..."
300$LDAPWHOAMI -H $URI1 \
301	-D "$BJORNSDN" -w $NEWPW
302RC=$?
303if test $RC != 0 ; then
304	echo "ldapwhoami failed ($RC)!"
305	test $KILLSERVERS != no && kill -HUP $KILLPIDS
306	exit $RC
307fi
308
309# ITS#57??
310$LDAPADD -H $URI1 \
311	-D "$MANAGERDN" -w secret \
312	>> $TESTOUT 2>&1 \
313	<< EOMODS
314dn: ou=Can't Contact,dc=example,dc=com
315changetype: add
316objectclass: referral
317objectclass: extensibleobject
318ou: Can't Contact
319# invalid URI to test broken connectivity handling (search only)
320ref: ${URI3}ou=Can't%20Contact,dc=example,dc=com
321EOMODS
322
323echo "Reading the referral entry \"ou=Can't Contact,$BASEDN\" as anonymous on port $PORT1..."
324$LDAPSEARCH -H $URI1 -b "$BASEDN" -S "" "(cn=Can't Contact)" \
325	 > $SEARCHOUT 2>&1
326
327RC=$?
328if test $RC != 0 ; then
329	echo "ldapsearch failed ($RC)!"
330	test $KILLSERVERS != no && kill -HUP $KILLPIDS
331	exit $RC
332fi
333
334test $KILLSERVERS != no && kill -HUP $KILLPIDS
335
336echo ">>>>> Test succeeded"
337
338test $KILLSERVERS != no && wait
339
340exit 0
341