xref: /netbsd-src/external/bsd/openldap/dist/tests/scripts/test064-constraint (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>.
15d11b170bStron
16d11b170bStronecho "running defines.sh"
17d11b170bStron. $SRCDIR/scripts/defines.sh
18d11b170bStron
19d11b170bStronif test $CONSTRAINT = constraintno; then
20d11b170bStron	echo "Constraint overlay not available, test skipped"
21d11b170bStron	exit 0
22d11b170bStronfi
23d11b170bStron
24d11b170bStronCONSTRAINTDIR="$DATADIR/constraint"
25d11b170bStronROOTLDIF="$CONSTRAINTDIR/root.ldif"
26d11b170bStronUSERLDIF="$CONSTRAINTDIR/user.ldif"
27d11b170bStronRESULTOUT="$CONSTRAINTDIR/constraint.out"
28d11b170bStronSCRIPTOUT="$TESTDIR/constraint.out"
29d11b170bStronUSERDN="cn=John Doe,ou=users,$BASEDN"
30d11b170bStron
31d11b170bStronCONFDIR=$TESTDIR/slapd.d
32d11b170bStronmkdir -p $TESTDIR $CONFDIR $DBDIR1
33d11b170bStron
34d11b170bStron$SLAPPASSWD -g -n >$CONFIGPWF
35d11b170bStron
36d11b170bStroncat > $TESTDIR/config.ldif <<EOF
37d11b170bStrondn: cn=config
38d11b170bStronobjectClass: olcGlobal
39d11b170bStroncn: config
40d11b170bStronolcArgsFile: $TESTDIR/slapd.args
41d11b170bStronolcPidFile: $TESTDIR/slapd.pid
42d11b170bStron
43d11b170bStrondn: cn=schema,cn=config
44d11b170bStronobjectClass: olcSchemaConfig
45d11b170bStroncn: schema
46d11b170bStron
47d11b170bStroninclude: file://$TESTWD/schema/core.ldif
48d11b170bStroninclude: file://$TESTWD/schema/cosine.ldif
49d11b170bStroninclude: file://$TESTWD/schema/inetorgperson.ldif
50d11b170bStron
51d11b170bStrondn: olcDatabase=config,cn=config
52d11b170bStronobjectClass: olcDatabaseConfig
53d11b170bStronolcDatabase: config
54d11b170bStronolcRootPW:< file://$CONFIGPWF
55d11b170bStronEOF
56d11b170bStron
57d11b170bStronif [ "$BACKENDTYPE" = mod ]; then
58d11b170bStron	cat >> $TESTDIR/config.ldif <<EOF
59d11b170bStron
60d11b170bStrondn: cn=module,cn=config
61d11b170bStronobjectClass: olcModuleList
62d11b170bStroncn: module
63d11b170bStronolcModulePath: $TESTWD/../servers/slapd/back-$BACKEND
64d11b170bStronolcModuleLoad: back_$BACKEND.la
65d11b170bStronEOF
66d11b170bStronfi
67d11b170bStron
68d11b170bStronif [ "$CONSTRAINT" = constraintmod ]; then
69d11b170bStron	cat >> $TESTDIR/config.ldif <<EOF
70d11b170bStron
71d11b170bStrondn: cn=module,cn=config
72d11b170bStronobjectClass: olcModuleList
73d11b170bStroncn: module
74d11b170bStronolcModulePath: $TESTWD/../servers/slapd/overlays
75d11b170bStronolcModuleLoad: constraint.la
76d11b170bStronEOF
77d11b170bStronfi
78d11b170bStron
79d11b170bStroncat >> $TESTDIR/config.ldif <<EOF
80d11b170bStron
81d11b170bStrondn: olcDatabase={1}$BACKEND,cn=config
82d11b170bStronobjectClass: olcDatabaseConfig
83d11b170bStronobjectClass: olc${BACKEND}Config
84d11b170bStronolcDatabase: $BACKEND
85d11b170bStronolcSuffix: $BASEDN
86d11b170bStronolcRootDN: $MANAGERDN
87d11b170bStronolcRootPW: $PASSWD
88d11b170bStronolcDbDirectory: $TESTDIR/db.1.a
89d11b170bStronEOF
90d11b170bStron
91d11b170bStronif [ "$INDEXDB" = indexdb ]; then
92d11b170bStron	cat >> $TESTDIR/config.ldif <<EOF
93d11b170bStronolcDbIndex: objectClass eq,pres
94d11b170bStronolcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
95d11b170bStronEOF
96d11b170bStronfi
97d11b170bStron
98d11b170bStroncat >> $TESTDIR/config.ldif <<EOF
99d11b170bStron
100d11b170bStrondn: olcOverlay=constraint,olcDatabase={1}$BACKEND,cn=config
101d11b170bStronobjectClass: olcOverlayConfig
102d11b170bStronobjectClass: olcConstraintConfig
103d11b170bStronolcOverlay: constraint
104d11b170bStronolcConstraintAttribute: mail
105d11b170bStron  count 3
106d11b170bStron  restrict="ldap:///ou=users,$BASEDN??one?(objectClass=inetOrgPerson)"
107d11b170bStron# check if restrict works (if not, this will apply to ou=users subtree as well
108d11b170bStron# and some tests will fail)
109d11b170bStronolcConstraintAttribute: mail count 1 restrict="ldap:///ou=groups,$BASEDN??one"
110d11b170bStronolcConstraintAttribute: mail regex ^[[:alnum:]]+@example.com$
111d11b170bStronolcConstraintAttribute: description count 2
112376af7d7SchristosolcConstraintAttribute: jpegPhoto count 0
113d11b170bStron# cn value has to be concatenated givenName SP sn
114d11b170bStronolcConstraintAttribute: cn,sn,givenName
115d11b170bStron  set "(this/givenName + [ ] + this/sn) & this/cn"
116d11b170bStron  restrict="ldap:///$USERDN??sub?(objectClass=inetOrgPerson)"
117d11b170bStronolcConstraintAttribute: uid
118d11b170bStron  uri "ldap:///ou=groups,$BASEDN?uid?one?(objectClass=inetOrgPerson)"
119d11b170bStron  restrict="ldap:///ou=users,$BASEDN??one"
120d11b170bStronEOF
121d11b170bStron
122d11b170bStron$SLAPADD -F $CONFDIR -n 0 -l $TESTDIR/config.ldif
123d11b170bStron
124d11b170bStronecho "Starting slapd on TCP/IP port $PORT1..."
125*e670fd5cSchristos$SLAPD -F $CONFDIR -h $URI1 -d $LVL > $LOG1 2>&1 &
126d11b170bStronPID=$!
127d11b170bStronif test $WAIT != 0 ; then
128d11b170bStron	echo PID $PID
129d11b170bStron	read foo
130d11b170bStronfi
131648e71e5SchristosKILLPIDS="$PID"
132d11b170bStronsleep 1
133d11b170bStron
134648e71e5Schristosecho "Using ldapsearch to check that slapd is running..."
135648e71e5Schristosfor i in 0 1 2 3 4 5; do
136*e670fd5cSchristos	$LDAPSEARCH -s base -b "$MONITOR" -H $URI1 \
137648e71e5Schristos		'objectclass=*' > /dev/null 2>&1
138648e71e5Schristos	RC=$?
139648e71e5Schristos	if test $RC = 0 ; then
140648e71e5Schristos		break
141648e71e5Schristos	fi
142648e71e5Schristos	echo "Waiting 5 seconds for slapd to start..."
143648e71e5Schristos	sleep 5
144648e71e5Schristosdone
145648e71e5Schristosif test $RC != 0 ; then
146648e71e5Schristos	echo "ldapsearch failed ($RC)!"
147648e71e5Schristos	test $KILLSERVERS != no && kill -HUP $KILLPIDS
148648e71e5Schristos	exit $RC
149648e71e5Schristosfi
150648e71e5Schristos
151d11b170bStronecho "Adding basic structure..."
152*e670fd5cSchristos$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $ROOTLDIF >/dev/null 2>&1
153d11b170bStronRC=$?
154d11b170bStronif test $RC != 0 ; then
155d11b170bStron	echo "ldapadd failed ($RC)!"
156d11b170bStron	test $KILLSERVERS != no && kill -HUP $PID
157d11b170bStron	exit $RC
158d11b170bStronfi
159*e670fd5cSchristos$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $USERLDIF >/dev/null 2>&1
160d11b170bStronRC=$?
161d11b170bStronif test $RC != 0 ; then
162d11b170bStron	echo "ldapadd failed ($RC)!"
163d11b170bStron	test $KILLSERVERS != no && kill -HUP $PID
164d11b170bStron	exit $RC
165d11b170bStronfi
166d11b170bStron
167d11b170bStronecho "Running constraint tests..."
168*e670fd5cSchristosfor ldif in $CONSTRAINTDIR/*ok*.ldif $CONSTRAINTDIR/*fail*.ldif; do
169d11b170bStron	### reload
170*e670fd5cSchristos	$LDAPDELETE -D "$MANAGERDN" -H $URI1 -w $PASSWD "$USERDN" >/dev/null 2>&1
171d11b170bStron	RC=$?
172d11b170bStron	if test $RC != 0 ; then
173d11b170bStron		echo "ldapdelete failed ($RC)!"
174d11b170bStron		test $KILLSERVERS != no && kill -HUP $PID
175d11b170bStron		exit $RC
176d11b170bStron	fi
177*e670fd5cSchristos	$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $USERLDIF >/dev/null 2>&1
178d11b170bStron	RC=$?
179d11b170bStron	if test $RC != 0 ; then
180d11b170bStron		echo "ldapadd failed ($RC)!"
181d11b170bStron		test $KILLSERVERS != no && kill -HUP $PID
182d11b170bStron		exit $RC
183d11b170bStron	fi
184d11b170bStron
185d11b170bStron	### info
186d11b170bStron	echo -n "  [$ldif]: "
187d11b170bStron
188d11b170bStron	### modify
189*e670fd5cSchristos	$LDAPMODIFY -H $URI1 -x -D "$MANAGERDN" -f $ldif -w $PASSWD >/dev/null 2>&1
190d11b170bStron	RC=$?
191*e670fd5cSchristos	if test $RC = 0 ; then
192d11b170bStron		echo "OK" | tee -a $SCRIPTOUT
193*e670fd5cSchristos	elif test $RC = 19 ; then
194d11b170bStron		echo "FAIL" | tee -a $SCRIPTOUT
195d11b170bStron	else
196d11b170bStron		echo "UNEXPECTED ($RC)"
197d11b170bStron	fi
198d11b170bStrondone
199d11b170bStron
200d11b170bStronecho "Comparing output..."
201d11b170bStron$DIFF $SCRIPTOUT $RESULTOUT > $CMPOUT
202d11b170bStronRC=$?
203d11b170bStronif test $RC != 0 ; then
204d11b170bStron	echo "Comparison failed"
205d11b170bStron	test $KILLSERVERS != no && kill -HUP $PID
206d11b170bStron	exit $RC
207d11b170bStronfi
208d11b170bStron
209d11b170bStrontest $KILLSERVERS != no && kill -HUP $PID
210d11b170bStron
211d11b170bStronecho ">>>>> Test succeeded"
212d11b170bStron
213d11b170bStrontest $KILLSERVERS != no && wait
214d11b170bStron
215d11b170bStronexit 0
216