xref: /netbsd-src/external/bsd/openldap/dist/tests/scripts/test064-constraint (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1#!/bin/bash
2
3echo "running defines.sh"
4. $SRCDIR/scripts/defines.sh
5
6if test $CONSTRAINT = constraintno; then
7	echo "Constraint overlay not available, test skipped"
8	exit 0
9fi
10
11CONSTRAINTDIR="$DATADIR/constraint"
12ROOTLDIF="$CONSTRAINTDIR/root.ldif"
13USERLDIF="$CONSTRAINTDIR/user.ldif"
14RESULTOUT="$CONSTRAINTDIR/constraint.out"
15SCRIPTOUT="$TESTDIR/constraint.out"
16USERDN="cn=John Doe,ou=users,$BASEDN"
17
18CONFDIR=$TESTDIR/slapd.d
19mkdir -p $TESTDIR $CONFDIR $DBDIR1
20
21$SLAPPASSWD -g -n >$CONFIGPWF
22
23cat > $TESTDIR/config.ldif <<EOF
24dn: cn=config
25objectClass: olcGlobal
26cn: config
27olcArgsFile: $TESTDIR/slapd.args
28olcPidFile: $TESTDIR/slapd.pid
29
30dn: cn=schema,cn=config
31objectClass: olcSchemaConfig
32cn: schema
33
34include: file://$TESTWD/schema/core.ldif
35include: file://$TESTWD/schema/cosine.ldif
36include: file://$TESTWD/schema/inetorgperson.ldif
37
38dn: olcDatabase=config,cn=config
39objectClass: olcDatabaseConfig
40olcDatabase: config
41olcRootPW:< file://$CONFIGPWF
42EOF
43
44if [ "$BACKENDTYPE" = mod ]; then
45	cat >> $TESTDIR/config.ldif <<EOF
46
47dn: cn=module,cn=config
48objectClass: olcModuleList
49cn: module
50olcModulePath: $TESTWD/../servers/slapd/back-$BACKEND
51olcModuleLoad: back_$BACKEND.la
52EOF
53fi
54
55if [ "$CONSTRAINT" = constraintmod ]; then
56	cat >> $TESTDIR/config.ldif <<EOF
57
58dn: cn=module,cn=config
59objectClass: olcModuleList
60cn: module
61olcModulePath: $TESTWD/../servers/slapd/overlays
62olcModuleLoad: constraint.la
63EOF
64fi
65
66cat >> $TESTDIR/config.ldif <<EOF
67
68dn: olcDatabase={1}$BACKEND,cn=config
69objectClass: olcDatabaseConfig
70objectClass: olc${BACKEND}Config
71olcDatabase: $BACKEND
72olcSuffix: $BASEDN
73olcRootDN: $MANAGERDN
74olcRootPW: $PASSWD
75olcDbDirectory: $TESTDIR/db.1.a
76EOF
77
78if [ "$INDEXDB" = indexdb ]; then
79	cat >> $TESTDIR/config.ldif <<EOF
80olcDbIndex: objectClass eq,pres
81olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
82EOF
83fi
84
85cat >> $TESTDIR/config.ldif <<EOF
86
87dn: olcOverlay=constraint,olcDatabase={1}$BACKEND,cn=config
88objectClass: olcOverlayConfig
89objectClass: olcConstraintConfig
90olcOverlay: constraint
91olcConstraintAttribute: mail
92  count 3
93  restrict="ldap:///ou=users,$BASEDN??one?(objectClass=inetOrgPerson)"
94# check if restrict works (if not, this will apply to ou=users subtree as well
95# and some tests will fail)
96olcConstraintAttribute: mail count 1 restrict="ldap:///ou=groups,$BASEDN??one"
97olcConstraintAttribute: mail regex ^[[:alnum:]]+@example.com$
98olcConstraintAttribute: description count 2
99# cn value has to be concatenated givenName SP sn
100olcConstraintAttribute: cn,sn,givenName
101  set "(this/givenName + [ ] + this/sn) & this/cn"
102  restrict="ldap:///$USERDN??sub?(objectClass=inetOrgPerson)"
103olcConstraintAttribute: uid
104  uri "ldap:///ou=groups,$BASEDN?uid?one?(objectClass=inetOrgPerson)"
105  restrict="ldap:///ou=users,$BASEDN??one"
106EOF
107
108$SLAPADD -F $CONFDIR -n 0 -l $TESTDIR/config.ldif
109
110echo "Starting slapd on TCP/IP port $PORT1..."
111$SLAPD -F $CONFDIR -h $URI1 -d $LVL $TIMING > $LOG1 2>&1 &
112PID=$!
113if test $WAIT != 0 ; then
114	echo PID $PID
115	read foo
116fi
117sleep 1
118
119echo "Adding basic structure..."
120$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $ROOTLDIF &>/dev/null
121RC=$?
122if test $RC != 0 ; then
123	echo "ldapadd failed ($RC)!"
124	test $KILLSERVERS != no && kill -HUP $PID
125	exit $RC
126fi
127$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $USERLDIF &>/dev/null
128RC=$?
129if test $RC != 0 ; then
130	echo "ldapadd failed ($RC)!"
131	test $KILLSERVERS != no && kill -HUP $PID
132	exit $RC
133fi
134
135echo "Running constraint tests..."
136for ldif in $SRCDIR/data/constraint/{*ok*,*fail*}.ldif; do
137	### reload
138	$LDAPDELETE -D "$MANAGERDN" -H $URI1 -w $PASSWD "$USERDN" &>/dev/null
139	RC=$?
140	if test $RC != 0 ; then
141		echo "ldapdelete failed ($RC)!"
142		test $KILLSERVERS != no && kill -HUP $PID
143		exit $RC
144	fi
145	$LDAPADD -D "$MANAGERDN" -H $URI1 -w $PASSWD -f $USERLDIF &>/dev/null
146	RC=$?
147	if test $RC != 0 ; then
148		echo "ldapadd failed ($RC)!"
149		test $KILLSERVERS != no && kill -HUP $PID
150		exit $RC
151	fi
152
153	### info
154	echo -n "  [$ldif]: "
155
156	### modify
157	$LDAPMODIFY -H $URI1 -x -D "$MANAGERDN" -f $ldif -w $PASSWD &>/dev/null
158	RC=$?
159	if test $RC == 0 ; then
160		echo "OK" | tee -a $SCRIPTOUT
161	elif test $RC == 19 ; then
162		echo "FAIL" | tee -a $SCRIPTOUT
163	else
164		echo "UNEXPECTED ($RC)"
165	fi
166done
167
168echo "Comparing output..."
169$DIFF $SCRIPTOUT $RESULTOUT > $CMPOUT
170RC=$?
171if test $RC != 0 ; then
172	echo "Comparison failed"
173	test $KILLSERVERS != no && kill -HUP $PID
174	exit $RC
175fi
176
177test $KILLSERVERS != no && kill -HUP $PID
178
179echo ">>>>> Test succeeded"
180
181test $KILLSERVERS != no && wait
182
183exit 0
184