xref: /netbsd-src/external/bsd/openldap/dist/tests/scripts/test066-autoca (revision e670fd5c413e99c2f6a37901bb21c537fcd322d2)
1*e670fd5cSchristos#! /bin/sh
2*e670fd5cSchristos# $OpenLDAP$
3*e670fd5cSchristos## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4*e670fd5cSchristos##
5*e670fd5cSchristos## Copyright 1998-2021 The OpenLDAP Foundation.
6*e670fd5cSchristos## All rights reserved.
7*e670fd5cSchristos##
8*e670fd5cSchristos## Redistribution and use in source and binary forms, with or without
9*e670fd5cSchristos## modification, are permitted only as authorized by the OpenLDAP
10*e670fd5cSchristos## Public License.
11*e670fd5cSchristos##
12*e670fd5cSchristos## A copy of this license is available in the file LICENSE in the
13*e670fd5cSchristos## top-level directory of the distribution or, alternatively, at
14*e670fd5cSchristos## <http://www.OpenLDAP.org/license.html>.
15*e670fd5cSchristos
16*e670fd5cSchristosecho "running defines.sh"
17*e670fd5cSchristos. $SRCDIR/scripts/defines.sh
18*e670fd5cSchristos
19*e670fd5cSchristosif test $AUTOCA = autocano; then
20*e670fd5cSchristos	echo "Automatic CA overlay not available, test skipped"
21*e670fd5cSchristos	exit 0
22*e670fd5cSchristosfi
23*e670fd5cSchristos
24*e670fd5cSchristosCFDIR=$TESTDIR/slapd.d
25*e670fd5cSchristos
26*e670fd5cSchristosmkdir -p $TESTDIR $CFDIR $DBDIR1
27*e670fd5cSchristos
28*e670fd5cSchristos$SLAPPASSWD -g -n >$CONFIGPWF
29*e670fd5cSchristos
30*e670fd5cSchristos#
31*e670fd5cSchristos# Test operation of autoca:
32*e670fd5cSchristos# - configure over ldap without TLS
33*e670fd5cSchristos# - populate over ldap
34*e670fd5cSchristos# - add host entry
35*e670fd5cSchristos# - add autoca overlay
36*e670fd5cSchristos# - generate server and user certs
37*e670fd5cSchristos# - check for TLS operation
38*e670fd5cSchristos#
39*e670fd5cSchristos
40*e670fd5cSchristosecho "Starting slapd on TCP/IP port $PORT1..."
41*e670fd5cSchristos. $CONFFILTER $BACKEND < $DYNAMICCONF > $CONFLDIF
42*e670fd5cSchristos$SLAPADD -F $CFDIR -n 0 -l $CONFLDIF
43*e670fd5cSchristos$SLAPD -F $CFDIR -h $URIP1 -d $LVL > $LOG1 2>&1 &
44*e670fd5cSchristosPID=$!
45*e670fd5cSchristosif test $WAIT != 0 ; then
46*e670fd5cSchristos    echo PID $PID
47*e670fd5cSchristos    read foo
48*e670fd5cSchristosfi
49*e670fd5cSchristosKILLPIDS="$PID"
50*e670fd5cSchristoscd $TESTWD
51*e670fd5cSchristos
52*e670fd5cSchristossleep 1
53*e670fd5cSchristos
54*e670fd5cSchristosecho "Using ldapsearch to check that slapd is running..."
55*e670fd5cSchristosfor i in 0 1 2 3 4 5; do
56*e670fd5cSchristos	$LDAPSEARCH -s base -b "" -H $URIP1 \
57*e670fd5cSchristos		'objectclass=*' > /dev/null 2>&1
58*e670fd5cSchristos	RC=$?
59*e670fd5cSchristos	if test $RC = 0 ; then
60*e670fd5cSchristos		break
61*e670fd5cSchristos	fi
62*e670fd5cSchristos	echo "Waiting 5 seconds for slapd to start..."
63*e670fd5cSchristos	sleep 5
64*e670fd5cSchristosdone
65*e670fd5cSchristos
66*e670fd5cSchristosif test $RC != 0 ; then
67*e670fd5cSchristos	echo "ldapsearch failed ($RC)!"
68*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
69*e670fd5cSchristos	exit $RC
70*e670fd5cSchristosfi
71*e670fd5cSchristos
72*e670fd5cSchristosecho "Adding schema and databases on slapd..."
73*e670fd5cSchristos$LDAPADD -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >>$TESTOUT 2>&1
74*e670fd5cSchristosinclude: file://$ABS_SCHEMADIR/core.ldif
75*e670fd5cSchristos
76*e670fd5cSchristosinclude: file://$ABS_SCHEMADIR/cosine.ldif
77*e670fd5cSchristos
78*e670fd5cSchristosinclude: file://$ABS_SCHEMADIR/inetorgperson.ldif
79*e670fd5cSchristos
80*e670fd5cSchristosinclude: file://$ABS_SCHEMADIR/openldap.ldif
81*e670fd5cSchristos
82*e670fd5cSchristosinclude: file://$ABS_SCHEMADIR/nis.ldif
83*e670fd5cSchristosEOF
84*e670fd5cSchristosRC=$?
85*e670fd5cSchristosif test $RC != 0 ; then
86*e670fd5cSchristos	echo "ldapadd failed for schema config ($RC)!"
87*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
88*e670fd5cSchristos	exit $RC
89*e670fd5cSchristosfi
90*e670fd5cSchristos
91*e670fd5cSchristosnullExclude="" nullOK=""
92*e670fd5cSchristostest $BACKEND = null && nullExclude="# " nullOK="OK"
93*e670fd5cSchristos
94*e670fd5cSchristosif [ "$BACKENDTYPE" = mod ]; then
95*e670fd5cSchristos	$LDAPADD -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >>$TESTOUT 2>&1
96*e670fd5cSchristosdn: cn=module,cn=config
97*e670fd5cSchristosobjectClass: olcModuleList
98*e670fd5cSchristoscn: module
99*e670fd5cSchristosolcModulePath: $TESTWD/../servers/slapd/back-$BACKEND
100*e670fd5cSchristosolcModuleLoad: back_$BACKEND.la
101*e670fd5cSchristosEOF
102*e670fd5cSchristos	RC=$?
103*e670fd5cSchristos	if test $RC != 0 ; then
104*e670fd5cSchristos		echo "ldapadd failed for backend config ($RC)!"
105*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
106*e670fd5cSchristos		exit $RC
107*e670fd5cSchristos	fi
108*e670fd5cSchristosfi
109*e670fd5cSchristos
110*e670fd5cSchristos$LDAPADD -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >>$TESTOUT 2>&1
111*e670fd5cSchristosdn: olcDatabase={1}$BACKEND,cn=config
112*e670fd5cSchristosobjectClass: olcDatabaseConfig
113*e670fd5cSchristos${nullExclude}objectClass: olc${BACKEND}Config
114*e670fd5cSchristosolcDatabase: {1}$BACKEND
115*e670fd5cSchristosolcSuffix: $BASEDN
116*e670fd5cSchristos${nullExclude}olcDbDirectory: $DBDIR1
117*e670fd5cSchristosolcRootDN: $MANAGERDN
118*e670fd5cSchristosolcRootPW: $PASSWD
119*e670fd5cSchristosEOF
120*e670fd5cSchristosRC=$?
121*e670fd5cSchristosif test $RC != 0 ; then
122*e670fd5cSchristos	echo "ldapadd failed for database config ($RC)!"
123*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
124*e670fd5cSchristos	exit $RC
125*e670fd5cSchristosfi
126*e670fd5cSchristos
127*e670fd5cSchristosif test $INDEXDB = indexdb ; then
128*e670fd5cSchristos	$LDAPMODIFY -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >>$TESTOUT 2>&1
129*e670fd5cSchristosdn: olcDatabase={1}$BACKEND,cn=config
130*e670fd5cSchristoschangetype: modify
131*e670fd5cSchristosadd: olcDbIndex
132*e670fd5cSchristosolcDbIndex: objectClass,entryUUID,entryCSN eq
133*e670fd5cSchristosolcDbIndex: cn,uid pres,eq,sub
134*e670fd5cSchristosEOF
135*e670fd5cSchristos	RC=$?
136*e670fd5cSchristos	if test $RC != 0 ; then
137*e670fd5cSchristos		echo "ldapadd modify for database config ($RC)!"
138*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
139*e670fd5cSchristos		exit $RC
140*e670fd5cSchristos	fi
141*e670fd5cSchristosfi
142*e670fd5cSchristos
143*e670fd5cSchristosecho "Using ldapadd to populate slapd..."
144*e670fd5cSchristos$LDAPADD -D "$MANAGERDN" -H $URIP1 -w $PASSWD -f $LDIFORDERED \
145*e670fd5cSchristos	>> $TESTOUT 2>&1
146*e670fd5cSchristosRC=$?
147*e670fd5cSchristosif test $RC != 0 ; then
148*e670fd5cSchristos	echo "ldapadd failed for database populate ($RC)!"
149*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
150*e670fd5cSchristos	exit $RC
151*e670fd5cSchristosfi
152*e670fd5cSchristos
153*e670fd5cSchristosecho "Adding server entries to slapd..."
154*e670fd5cSchristos$LDAPADD -D "$MANAGERDN" -H $URIP1 -w $PASSWD <<EOF >> $TESTOUT 2>&1
155*e670fd5cSchristosdn: ou=Servers,$BASEDN
156*e670fd5cSchristosobjectClass: organizationalUnit
157*e670fd5cSchristosou: Servers
158*e670fd5cSchristos
159*e670fd5cSchristosdn: cn=localhost,ou=Servers,$BASEDN
160*e670fd5cSchristosobjectClass: device
161*e670fd5cSchristosobjectClass: ipHost
162*e670fd5cSchristoscn: localhost
163*e670fd5cSchristosipHostNumber: 127.0.0.1
164*e670fd5cSchristos
165*e670fd5cSchristosdn: cn=www.example.com,ou=Servers,$BASEDN
166*e670fd5cSchristosobjectClass: device
167*e670fd5cSchristosobjectClass: ipHost
168*e670fd5cSchristoscn: localhost
169*e670fd5cSchristosipHostNumber: 93.184.216.34
170*e670fd5cSchristosEOF
171*e670fd5cSchristosRC=$?
172*e670fd5cSchristosif test $RC != 0 ; then
173*e670fd5cSchristos	echo "ldapadd failed for database populate ($RC)!"
174*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
175*e670fd5cSchristos	exit $RC
176*e670fd5cSchristosfi
177*e670fd5cSchristos
178*e670fd5cSchristosecho "Inserting autoca overlay on slapd..."
179*e670fd5cSchristosif [ "$AUTOCA" = autocamod ]; then
180*e670fd5cSchristos	$LDAPADD -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF > $TESTOUT 2>&1
181*e670fd5cSchristosdn: cn=module,cn=config
182*e670fd5cSchristosobjectClass: olcModuleList
183*e670fd5cSchristoscn: module
184*e670fd5cSchristosolcModulePath: $TESTWD/../servers/slapd/overlays
185*e670fd5cSchristosolcModuleLoad: autoca.la
186*e670fd5cSchristosEOF
187*e670fd5cSchristos	RC=$?
188*e670fd5cSchristos	if test $RC != 0 ; then
189*e670fd5cSchristos		echo "ldapadd failed for moduleLoad ($RC)!"
190*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
191*e670fd5cSchristos		exit $RC
192*e670fd5cSchristos	fi
193*e670fd5cSchristosfi
194*e670fd5cSchristos$LDAPMODIFY -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
195*e670fd5cSchristosdn: olcOverlay=autoca,olcDatabase={1}$BACKEND,cn=config
196*e670fd5cSchristoschangetype: add
197*e670fd5cSchristosobjectClass: olcOverlayConfig
198*e670fd5cSchristosobjectClass: olcAutoCAConfig
199*e670fd5cSchristosolcOverlay: autoca
200*e670fd5cSchristosolcAutoCAlocalDN: cn=localhost,ou=Servers,$BASEDN
201*e670fd5cSchristosEOF
202*e670fd5cSchristosRC=$?
203*e670fd5cSchristosif test $RC != 0 ; then
204*e670fd5cSchristos	echo "ldapmodify failed for autoca config ($RC)!"
205*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
206*e670fd5cSchristos	exit $RC
207*e670fd5cSchristosfi
208*e670fd5cSchristosecho "Using ldapsearch to retrieve CA cert..."
209*e670fd5cSchristos$LDAPSEARCH -b $BASEDN -D $MANAGERDN -H $URIP1 -w $PASSWD -s base \
210*e670fd5cSchristos	'objectclass=*' 'cACertificate;binary'  > $SEARCHOUT 2>&1
211*e670fd5cSchristosRC=$?
212*e670fd5cSchristos
213*e670fd5cSchristosif test $RC != 0 ; then
214*e670fd5cSchristos	echo "ldapsearch failed ($RC)!"
215*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
216*e670fd5cSchristos	exit $RC
217*e670fd5cSchristosfi
218*e670fd5cSchristos
219*e670fd5cSchristosecho "Setting up CA cert..."
220*e670fd5cSchristosecho "-----BEGIN CERTIFICATE-----" > $TESTDIR/cacert.pem
221*e670fd5cSchristossed -e "/^dn:/d" -e "s/cACertificate;binary:://" -e "/^$/d" $SEARCHOUT >> $TESTDIR/cacert.pem
222*e670fd5cSchristosecho "-----END CERTIFICATE-----" >> $TESTDIR/cacert.pem
223*e670fd5cSchristos
224*e670fd5cSchristosecho "Using ldapsearch to generate localhost cert..."
225*e670fd5cSchristos$LDAPSEARCH -b cn=localhost,ou=Servers,$BASEDN -D $MANAGERDN -H $URIP1 -w $PASSWD -s base \
226*e670fd5cSchristos	-A 'objectclass=*' 'userCertificate;binary' 'userPrivateKey;binary'  >> $TESTOUT 2>&1
227*e670fd5cSchristosRC=$?
228*e670fd5cSchristos
229*e670fd5cSchristosif test $RC != 0 ; then
230*e670fd5cSchristos	echo "ldapsearch failed ($RC)!"
231*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
232*e670fd5cSchristos	exit $RC
233*e670fd5cSchristosfi
234*e670fd5cSchristos
235*e670fd5cSchristosecho "Using ldapsearch to attempt TLS..."
236*e670fd5cSchristosunset LDAPNOINIT
237*e670fd5cSchristosLDAPTLS_CACERT=$TESTDIR/cacert.pem
238*e670fd5cSchristosexport LDAPTLS_CACERT
239*e670fd5cSchristos$LDAPSEARCH -b $BASEDN -D $MANAGERDN -H $URIP1 -w $PASSWD -s base -ZZ \
240*e670fd5cSchristos	'objectclass=*' >> $TESTOUT 2>&1
241*e670fd5cSchristosRC=$?
242*e670fd5cSchristos
243*e670fd5cSchristosif test $RC != 0 ; then
244*e670fd5cSchristos	echo "ldapsearch failed ($RC)!"
245*e670fd5cSchristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
246*e670fd5cSchristos	exit $RC
247*e670fd5cSchristosfi
248*e670fd5cSchristos
249*e670fd5cSchristosif test $WITH_SASL = no ; then
250*e670fd5cSchristos	echo "SASL support not available, skipping client cert authentication"
251*e670fd5cSchristoselse
252*e670fd5cSchristos	# note - the attrs are being saved in raw DER form.
253*e670fd5cSchristos	# they need to be base64 encoded into PEM for most programs to use them
254*e670fd5cSchristos	# so we ignore those files for now.
255*e670fd5cSchristos	echo "Using ldapsearch to generate user cert..."
256*e670fd5cSchristos	$LDAPSEARCH -b "$BABSDN" -D $MANAGERDN -H $URIP1 -w $PASSWD -s base -ZZ \
257*e670fd5cSchristos		-T $TESTDIR -t 'objectclass=*' 'userCertificate;binary' 'userPrivateKey;binary'  >> $TESTOUT 2>&1
258*e670fd5cSchristos	RC=$?
259*e670fd5cSchristos
260*e670fd5cSchristos	if test $RC != 0 ; then
261*e670fd5cSchristos		echo "ldapsearch failed ($RC)!"
262*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
263*e670fd5cSchristos		exit $RC
264*e670fd5cSchristos	fi
265*e670fd5cSchristos
266*e670fd5cSchristos	echo "Using ldapsearch to retrieve user cert..."
267*e670fd5cSchristos	$LDAPSEARCH -b "$BABSDN" -D $MANAGERDN -H $URIP1 -w $PASSWD -s base -ZZ \
268*e670fd5cSchristos		'objectclass=*' 'userCertificate;binary' > $SEARCHOUT 2>&1
269*e670fd5cSchristos	RC=$?
270*e670fd5cSchristos
271*e670fd5cSchristos	if test $RC != 0 ; then
272*e670fd5cSchristos		echo "ldapsearch failed ($RC)!"
273*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
274*e670fd5cSchristos		exit $RC
275*e670fd5cSchristos	fi
276*e670fd5cSchristos
277*e670fd5cSchristos	echo "Setting up user cert..."
278*e670fd5cSchristos	echo "-----BEGIN CERTIFICATE-----" > $TESTDIR/usercert.pem
279*e670fd5cSchristos	sed -e "/^dn:/d" -e "/^ dc=com/d" -e "s/userCertificate;binary:://" -e "/^$/d" $SEARCHOUT >> $TESTDIR/usercert.pem
280*e670fd5cSchristos	echo "-----END CERTIFICATE-----" >> $TESTDIR/usercert.pem
281*e670fd5cSchristos
282*e670fd5cSchristos	echo "Using ldapsearch to retrieve user key..."
283*e670fd5cSchristos	$LDAPSEARCH -b "$BABSDN" -D $MANAGERDN -H $URIP1 -w $PASSWD -s base -ZZ \
284*e670fd5cSchristos		'objectclass=*' 'userPrivateKey;binary' > $SEARCHOUT 2>&1
285*e670fd5cSchristos	RC=$?
286*e670fd5cSchristos
287*e670fd5cSchristos	if test $RC != 0 ; then
288*e670fd5cSchristos		echo "ldapsearch failed ($RC)!"
289*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
290*e670fd5cSchristos		exit $RC
291*e670fd5cSchristos	fi
292*e670fd5cSchristos
293*e670fd5cSchristos	echo "Setting up user key..."
294*e670fd5cSchristos	echo "-----BEGIN PRIVATE KEY-----" > $TESTDIR/userkey.pem
295*e670fd5cSchristos	sed -e "/^dn:/d" -e "/^ dc=com/d" -e "s/userPrivateKey;binary:://" -e "/^$/d" $SEARCHOUT >> $TESTDIR/userkey.pem
296*e670fd5cSchristos	echo "-----END PRIVATE KEY-----" >> $TESTDIR/userkey.pem
297*e670fd5cSchristos
298*e670fd5cSchristos	LDAPTLS_CERT=$TESTDIR/usercert.pem
299*e670fd5cSchristos	LDAPTLS_KEY=$TESTDIR/userkey.pem
300*e670fd5cSchristos	export LDAPTLS_CERT
301*e670fd5cSchristos	export LDAPTLS_KEY
302*e670fd5cSchristos
303*e670fd5cSchristos	echo "Setting TLSVerifyClient to try..."
304*e670fd5cSchristos	$LDAPMODIFY -D cn=config -H $URIP1 -y $CONFIGPWF <<EOF >> $TESTOUT 2>&1
305*e670fd5cSchristosdn: cn=config
306*e670fd5cSchristoschangetype: modify
307*e670fd5cSchristosreplace: olcTLSVerifyClient
308*e670fd5cSchristosolcTLSVerifyClient: try
309*e670fd5cSchristosEOF
310*e670fd5cSchristos	RC=$?
311*e670fd5cSchristos	if test $RC != 0 ; then
312*e670fd5cSchristos		echo "ldapmodify failed for autoca config ($RC)!"
313*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
314*e670fd5cSchristos		exit $RC
315*e670fd5cSchristos	fi
316*e670fd5cSchristos
317*e670fd5cSchristos	$CLIENTDIR/ldapwhoami -Y EXTERNAL -H $URIP1 -ZZ
318*e670fd5cSchristos
319*e670fd5cSchristos	if test $RC != 0 ; then
320*e670fd5cSchristos		echo "ldapwhoami failed ($RC)!"
321*e670fd5cSchristos		test $KILLSERVERS != no && kill -HUP $KILLPIDS
322*e670fd5cSchristos		exit $RC
323*e670fd5cSchristos	fi
324*e670fd5cSchristosfi
325*e670fd5cSchristos
326*e670fd5cSchristostest $KILLSERVERS != no && kill -HUP $KILLPIDS
327*e670fd5cSchristos
328*e670fd5cSchristosecho ">>>>> Test succeeded"
329*e670fd5cSchristos
330*e670fd5cSchristostest $KILLSERVERS != no && wait
331*e670fd5cSchristos
332*e670fd5cSchristosexit 0
333