1#! /bin/sh
2## $OpenLDAP$
3## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4##
5## Copyright 2016-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##
16## ACKNOWLEDGEMENTS:
17## This module was written in 2016 by Ond��ej Kuzn��k for Symas Corp.
18
19echo "running defines.sh"
20. $SRCDIR/scripts/defines.sh
21
22. ${SCRIPTDIR}/common.sh
23
24echo "Applying invalid changes to config (should fail)..."
25for CHANGE in data/test001-*.ldif; do
26	echo "... $CHANGE"
27	. $CONFFILTER $BACKEND $MONITORDB < $CHANGE | \
28	$LDAPMODIFY -D cn=config -H $URI1 -y $CONFIGPWF \
29		>> $TESTOUT 2>&1
30	RC=$?
31	case $RC in
32	0)
33		echo "ldapmodify should have failed ($RC)!"
34		test $KILLSERVERS != no && kill -HUP $KILLPIDS
35		exit 1
36		;;
37	80)
38		echo "ldapmodify failed ($RC)"
39		;;
40	*)
41		echo "ldapmodify failed ($RC)!"
42		test $KILLSERVERS != no && kill -HUP $KILLPIDS
43		exit $RC
44		;;
45	esac
46done
47
48# We run this search after the changes above and before restart so we can also
49# check the reconfiguration attempts actually had no side effects
50echo "Saving search output before server restart..."
51echo "# search output from dynamically configured server..." >> $SERVER6OUT
52$LDAPSEARCH -b "$BASEDN" -H $URI1 \
53	>> $SERVER6OUT 2>&1
54RC=$?
55if test $RC != 0 ; then
56	echo "ldapsearch failed ($RC)!"
57	test $KILLSERVERS != no && kill -HUP $KILLPIDS
58	exit $RC
59fi
60
61echo "Stopping slapd on TCP/IP port $PORT1..."
62kill -HUP $KILLPIDS
63KILLPIDS=""
64sleep $SLEEP0
65echo "Starting slapd on TCP/IP port $PORT1..."
66$SLAPD -F $TESTDIR/confdir -h $URI1 -d $LVL >> $LOG1 2>&1 &
67PID=$!
68if test $WAIT != 0 ; then
69	echo PID $PID
70	read foo
71fi
72KILLPIDS="$PID"
73
74sleep $SLEEP0
75
76for i in 0 1 2 3 4 5; do
77	$LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
78		'objectclass=*' > /dev/null 2>&1
79	RC=$?
80	if test $RC = 0 ; then
81		break
82	fi
83	echo "Waiting ${SLEEP1} seconds for slapd to start..."
84	sleep ${SLEEP1}
85done
86
87echo "Testing slapd.conf support..."
88mkdir $TESTDIR/conftest $DBDIR2
89. $CONFFILTER $BACKEND $MONITORDB < $CONFTWO \
90	| sed -e '/^argsfile.*/a\
91moduleload ../datamorph.la' \
92		-e '/database.*monitor/i\
93include data/datamorph.conf' \
94	> $CONF2
95echo "database config" >>$CONF2
96echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >>$CONF2
97
98$SLAPADD -f $CONF2 -l data/test.ldif
99RC=$?
100if test $RC != 0 ; then
101	echo "slapadd failed ($RC)!"
102	test $KILLSERVERS != no && kill -HUP $KILLPIDS
103	exit $RC
104fi
105
106echo "Starting slapd on TCP/IP port $PORT2..."
107$SLAPD -f $CONF2 -h $URI2 -d $LVL >> $LOG2 2>&1 &
108PID=$!
109if test $WAIT != 0 ; then
110	echo PID $PID
111	read foo
112fi
113
114sleep $SLEEP0
115
116for i in 0 1 2 3 4 5; do
117	$LDAPSEARCH -s base -b "$MONITOR" -H $URI2 \
118		'objectclass=*' > /dev/null 2>&1
119	RC=$?
120	if test $RC = 0 ; then
121		break
122	fi
123	echo "Waiting ${SLEEP1} seconds for slapd to start..."
124	sleep ${SLEEP1}
125done
126
127echo "# search output from server running from slapd.conf..." >> $SERVER2OUT
128$LDAPSEARCH -b "$BASEDN" -H $URI2 \
129	>> $SERVER2OUT 2>&1
130RC=$?
131if test $RC != 0 ; then
132	echo "ldapsearch failed ($RC)!"
133	test $KILLSERVERS != no && kill -HUP $KILLPIDS
134	exit $RC
135fi
136
137echo "Stopping slapd on TCP/IP port $PORT2..."
138kill -HUP $PID
139
140$SLAPD -Tt -f $CONF2 -F $TESTDIR/conftest -d $LVL >> $LOG3 2>&1
141
142echo "Starting slapd on TCP/IP port $PORT2..."
143$SLAPD -F $TESTDIR/conftest -h $URI2 -d $LVL >> $LOG3 2>&1 &
144PID=$!
145if test $WAIT != 0 ; then
146	echo PID $PID
147	read foo
148fi
149KILLPIDS="$KILLPIDS $PID"
150
151sleep $SLEEP0
152
153for i in 0 1 2 3 4 5; do
154	$LDAPSEARCH -s base -b "$MONITOR" -H $URI2 \
155		'objectclass=*' > /dev/null 2>&1
156	RC=$?
157	if test $RC = 0 ; then
158		break
159	fi
160	echo "Waiting ${SLEEP1} seconds for slapd to start..."
161	sleep ${SLEEP1}
162done
163
164echo "Gathering overlay configuration from both servers..."
165echo "# overlay configuration from dynamically configured server..." >> $SERVER1OUT
166$LDAPSEARCH -D cn=config -H $URI1 -y $CONFIGPWF \
167	-b "olcOverlay={0}datamorph,olcDatabase={1}$BACKEND,cn=config" \
168	| sed -e "s/ {[0-9]*}/ /" -e "s/={[0-9]*}/=/g" \
169	>> $SERVER1OUT 2>&1
170RC=$?
171if test $RC != 0 ; then
172	echo "ldapsearch failed ($RC)!"
173	test $KILLSERVERS != no && kill -HUP $KILLPIDS
174	exit $RC
175fi
176
177echo "# overlay configuration from server configured from slapd.conf..." >> $SERVER3OUT
178$LDAPSEARCH -D cn=config -H $URI2 -y $CONFIGPWF \
179	-b "olcOverlay={0}datamorph,olcDatabase={1}$BACKEND,cn=config" \
180	| sed -e "s/ {[0-9]*}/ /" -e "s/={[0-9]*}/=/g" \
181	>> $SERVER3OUT 2>&1
182RC=$?
183if test $RC != 0 ; then
184	echo "ldapsearch failed ($RC)!"
185	test $KILLSERVERS != no && kill -HUP $KILLPIDS
186	exit $RC
187fi
188
189# We've already filtered out the ordering markers, now sort the entries
190echo "Filtering ldapsearch results..."
191$LDIFFILTER -s e < $SERVER3OUT > $SERVER3FLT
192echo "Filtering expected entries..."
193$LDIFFILTER -s e < $SERVER1OUT > $SERVER1FLT
194echo "Comparing filter output..."
195$CMP $SERVER3FLT $SERVER1FLT > $CMPOUT
196
197if test $? != 0 ; then
198	echo "Comparison failed"
199	test $KILLSERVERS != no && kill -HUP $KILLPIDS
200	exit 1
201fi
202
203rm $SERVER1OUT $SERVER3OUT
204
205echo "Comparing search output on both servers..."
206echo "# search output from dynamically configured server..." >> $SERVER1OUT
207$LDAPSEARCH -b "$BASEDN" -H $URI1 \
208	>> $SERVER1OUT 2>&1
209RC=$?
210if test $RC != 0 ; then
211	echo "ldapsearch failed ($RC)!"
212	test $KILLSERVERS != no && kill -HUP $KILLPIDS
213	exit $RC
214fi
215
216echo "# search output from server configured from slapd.conf..." >> $SERVER3OUT
217$LDAPSEARCH -b "$BASEDN" -H $URI2 \
218	>> $SERVER3OUT 2>&1
219RC=$?
220if test $RC != 0 ; then
221	echo "ldapsearch failed ($RC)!"
222	test $KILLSERVERS != no && kill -HUP $KILLPIDS
223	exit $RC
224fi
225
226test $KILLSERVERS != no && kill -HUP $KILLPIDS
227
228echo "Filtering ldapsearch results..."
229$LDIFFILTER -s e < $SERVER1OUT > $SERVER1FLT
230$LDIFFILTER -s e < $SERVER2OUT > $SERVER2FLT
231$LDIFFILTER -s e < $SERVER3OUT > $SERVER3FLT
232echo "Filtering expected entries..."
233$LDIFFILTER -s e < $SERVER6OUT > $SERVER6FLT
234echo "Comparing filter output..."
235$CMP $SERVER6FLT $SERVER1FLT > $CMPOUT && \
236$CMP $SERVER6FLT $SERVER2FLT > $CMPOUT && \
237$CMP $SERVER6FLT $SERVER3FLT > $CMPOUT
238
239if test $? != 0 ; then
240	echo "Comparison failed"
241	exit 1
242fi
243
244echo ">>>>> Test succeeded"
245
246test $KILLSERVERS != no && wait
247
248exit 0
249