xref: /netbsd-src/external/bsd/openldap/dist/tests/scripts/test024-unique (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1#! /bin/sh
2# OpenLDAP: pkg/ldap/tests/scripts/test024-unique,v 1.8.2.7 2009/07/27 18:39:18 quanah Exp
3## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4##
5## Copyright 2004-2009 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 $UNIQUE = uniqueno; then
20	echo "Attribute Uniqueness overlay not available, test skipped"
21	exit 0
22fi
23
24mkdir -p $TESTDIR $DBDIR1
25
26$SLAPPASSWD -g -n >$CONFIGPWF
27echo "rootpw `$SLAPPASSWD -T $CONFIGPWF`" >$TESTDIR/configpw.conf
28
29echo "Running slapadd to build slapd database..."
30. $CONFFILTER $BACKEND $MONITORDB < $UNIQUECONF > $CONF1
31$SLAPADD -f $CONF1 -l $LDIFUNIQUE
32RC=$?
33if test $RC != 0 ; then
34	echo "slapadd failed ($RC)!"
35	exit $RC
36fi
37
38echo "Starting slapd on TCP/IP port $PORT1..."
39mkdir $TESTDIR/confdir
40$SLAPD -f $CONF1 -F $TESTDIR/confdir -h $URI1 -d $LVL $TIMING > $LOG1 2>&1 &
41PID=$!
42if test $WAIT != 0 ; then
43    echo PID $PID
44    read foo
45fi
46KILLPIDS="$PID"
47
48sleep 1
49
50echo "Testing slapd attribute uniqueness operations..."
51for i in 0 1 2 3 4 5; do
52	$LDAPSEARCH -s base -b "$MONITOR" -h $LOCALHOST -p $PORT1 \
53		'objectclass=*' > /dev/null 2>&1
54	RC=$?
55	if test $RC = 0 ; then
56		break
57	fi
58	echo "Waiting 5 seconds for slapd to start..."
59	sleep 5
60done
61
62if test $RC != 0 ; then
63	echo "ldapsearch failed ($RC)!"
64	test $KILLSERVERS != no && kill -HUP $KILLPIDS
65	exit $RC
66fi
67
68echo "Adding a unique record..."
69$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD \
70	> /dev/null << EOTUNIQ1
71dn: uid=dave,ou=users,o=unique
72objectClass: inetOrgPerson
73uid: dave
74sn: nothere
75cn: dave
76businessCategory: otest
77carLicense: TEST
78departmentNumber: 42
79# NOTE: use special chars in attr value to be used
80# in internal searches ITS#4212
81displayName: Dave (ITS#4212)
82employeeNumber: 69
83employeeType: contractor
84givenName: Dave
85EOTUNIQ1
86RC=$?
87if test $RC != 0 ; then
88	echo "ldapadd failed ($RC)!"
89	test $KILLSERVERS != no && kill -HUP $KILLPIDS
90	exit $RC
91fi
92
93echo "Adding a non-unique record..."
94$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
95	 $TESTOUT 2>&1 << EOTUNIQ2
96dn: uid=bill,ou=users,o=unique
97objectClass: inetOrgPerson
98uid: bill
99sn: johnson
100cn: bill
101businessCategory: rtest
102carLicense: ABC123
103departmentNumber: 42
104displayName: Bill
105employeeNumber: 5150
106employeeType: contractor
107givenName: Bill
108EOTUNIQ2
109RC=$?
110if test $RC != 19 ; then
111	echo "unique check failed ($RC)!"
112	test $KILLSERVERS != no && kill -HUP $KILLPIDS
113	exit -1
114fi
115
116echo Dynamically retrieving initial configuration...
117$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/initial-config.ldif
118cat <<EOF >$TESTDIR/initial-reference.ldif
119dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
120objectClass: olcOverlayConfig
121objectClass: olcUniqueConfig
122olcOverlay: {0}unique
123olcUniqueBase: o=unique
124olcUniqueAttribute: employeeNumber
125olcUniqueAttribute: displayName
126
127EOF
128diff $TESTDIR/initial-config.ldif $TESTDIR/initial-reference.ldif > /dev/null 2>&1
129RC=$?
130if test $RC != 0 ; then
131    echo "Initial configuration is not reported correctly."
132    test $KILLSERVERS != no && kill -HUP $KILLPIDS
133    exit -1
134fi
135
136echo Dynamically trying to add a URI with legacy attrs present...
137$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
138    > $TESTOUT 2>&1 <<EOF
139dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
140changetype: modify
141add: olcUniqueURI
142olcUniqueURI: ldap:///?employeeNumber,displayName?sub
143EOF
144RC=$?
145if test $RC != 80 ; then
146	echo "legacy and unique_uri allowed together"
147	test $KILLSERVERS != no && kill -HUP $KILLPIDS
148	exit -1
149fi
150
151echo Dynamically trying to add legacy ignored attrs with legacy attrs present...
152$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
153    > $TESTOUT 2>&1 <<EOF
154dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
155changetype: modify
156add: olcUniqueIgnore
157olcUniqueIgnore: objectClass
158EOF
159RC=$?
160if test $RC != 80 ; then
161	echo "legacy attrs and legacy ignore attrs allowed together"
162	test $KILLSERVERS != no && kill -HUP $KILLPIDS
163	exit -1
164fi
165
166echo Verifying initial configuration intact...
167$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/initial-config-recheck.ldif
168diff $TESTDIR/initial-config-recheck.ldif $TESTDIR/initial-reference.ldif > /dev/null 2>&1
169RC=$?
170if test $RC != 0 ; then
171    echo "Initial configuration damaged by unsuccessful modifies."
172    test $KILLSERVERS != no && kill -HUP $KILLPIDS
173    exit -1
174fi
175
176echo Dynamically removing legacy base...
177$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
178    > $TESTOUT 2>&1 <<EOF
179dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
180changetype: modify
181delete: olcUniqueBase
182EOF
183RC=$?
184if test $RC != 0 ; then
185	echo "base removal failed"
186	test $KILLSERVERS != no && kill -HUP $KILLPIDS
187	exit -1
188fi
189
190echo Verifying base removal...
191$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/baseremoval-config.ldif
192cat >$TESTDIR/baseremoval-reference.ldif <<EOF
193dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
194objectClass: olcOverlayConfig
195objectClass: olcUniqueConfig
196olcOverlay: {0}unique
197olcUniqueAttribute: employeeNumber
198olcUniqueAttribute: displayName
199
200EOF
201diff $TESTDIR/baseremoval-config.ldif $TESTDIR/baseremoval-reference.ldif > /dev/null 2>&1
202RC=$?
203if test $RC != 0 ; then
204    echo "Configuration damaged by base removal"
205    test $KILLSERVERS != no && kill -HUP $KILLPIDS
206    exit -1
207fi
208
209echo "Adding a non-unique record..."
210$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
211	 $TESTOUT 2>&1 << EOTUNIQ2
212dn: uid=bill,ou=users,o=unique
213objectClass: inetOrgPerson
214uid: bill
215sn: johnson
216cn: bill
217businessCategory: rtest
218carLicense: ABC123
219departmentNumber: 42
220displayName: Bill
221employeeNumber: 5150
222employeeType: contractor
223givenName: Bill
224EOTUNIQ2
225RC=$?
226if test $RC != 19 ; then
227	echo "unique check failed ($RC)!"
228	test $KILLSERVERS != no && kill -HUP $KILLPIDS
229	exit -1
230fi
231
232echo Trying a legacy base outside of the backend...
233$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
234    > $TESTOUT 2>&1 <<EOF
235dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
236changetype: modify
237add: olcUniqueBase
238olcUniqueBase: cn=config
239EOF
240RC=$?
241if test $RC != 80 ; then
242	echo "out of backend scope base allowed"
243	test $KILLSERVERS != no && kill -HUP $KILLPIDS
244	exit -1
245fi
246
247echo "Adding and removing attrs..."
248$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
249    > $TESTOUT 2>&1 <<EOF
250dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
251changetype: modify
252add: olcUniqueAttribute
253olcUniqueAttribute: description
254olcUniqueAttribute: telephoneNumber
255-
256delete: olcUniqueAttribute
257olcUniqueAttribute: displayName
258EOF
259RC=$?
260if test $RC != 0 ; then
261	echo "Unable to remove an attribute"
262	test $KILLSERVERS != no && kill -HUP $KILLPIDS
263	exit -1
264fi
265
266echo "Verifying we removed the right attr..."
267$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
268	 $TESTOUT 2>&1 << EOTUNIQ2
269dn: uid=bill,ou=users,o=unique
270objectClass: inetOrgPerson
271uid: bill
272sn: johnson
273cn: bill
274businessCategory: rtest
275carLicense: ABC123
276departmentNumber: 42
277displayName: Bill
278employeeNumber: 5150
279employeeType: contractor
280givenName: Bill
281EOTUNIQ2
282RC=$?
283if test $RC != 19 ; then
284	echo "olcUniqueAttribtue single deletion hit the wrong value"
285	test $KILLSERVERS != no && kill -HUP $KILLPIDS
286	exit -1
287fi
288
289echo Removing legacy config and adding URIs...
290$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
291    > $TESTOUT 2>&1 <<EOF
292dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
293changetype: modify
294delete: olcUniqueAttribute
295-
296add: olcUniqueURI
297olcUniqueURI: ldap:///?employeeNumber,displayName?sub
298olcUniqueURI: ldap:///?description?one
299EOF
300RC=$?
301if test $RC != 0 ; then
302	echo "Reconfiguration to URIs failed"
303	test $KILLSERVERS != no && kill -HUP $KILLPIDS
304	exit -1
305fi
306
307echo Dynamically retrieving second configuration...
308$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/second-config.ldif
309cat >$TESTDIR/second-reference.ldif <<EOF
310dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
311objectClass: olcOverlayConfig
312objectClass: olcUniqueConfig
313olcOverlay: {0}unique
314olcUniqueURI: ldap:///?employeeNumber,displayName?sub
315olcUniqueURI: ldap:///?description?one
316
317EOF
318diff $TESTDIR/second-config.ldif $TESTDIR/second-reference.ldif > /dev/null 2>&1
319RC=$?
320if test $RC != 0 ; then
321    echo "Second configuration is not reported correctly."
322    test $KILLSERVERS != no && kill -HUP $KILLPIDS
323    exit -1
324fi
325
326echo "Adding a non-unique record..."
327$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
328	 $TESTOUT 2>&1 << EOTUNIQ2
329dn: uid=bill,ou=users,o=unique
330objectClass: inetOrgPerson
331uid: bill
332sn: johnson
333cn: bill
334businessCategory: rtest
335carLicense: ABC123
336departmentNumber: 42
337displayName: Bill
338employeeNumber: 5150
339employeeType: contractor
340givenName: Bill
341EOTUNIQ2
342RC=$?
343if test $RC != 19 ; then
344	echo "unique check failed ($RC)!"
345	test $KILLSERVERS != no && kill -HUP $KILLPIDS
346	exit -1
347fi
348
349echo Dynamically trying to add legacy base
350$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
351    > $TESTOUT 2>&1 <<EOF
352dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
353changetype: modify
354add: olcUniqueBase
355olcUniqueBase: o=unique
356EOF
357RC=$?
358if test $RC != 80 ; then
359	echo "legacy base allowed with URIs"
360	test $KILLSERVERS != no && kill -HUP $KILLPIDS
361	exit -1
362fi
363
364echo Dynamically trying to add legacy attrs
365$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
366    > $TESTOUT 2>&1 <<EOF
367dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
368changetype: modify
369add: olcUniqueAttribute
370olcUniqueAttribute: description
371EOF
372RC=$?
373if test $RC != 80 ; then
374	echo "legacy attributes allowed with URIs"
375	test $KILLSERVERS != no && kill -HUP $KILLPIDS
376	exit -1
377fi
378
379echo Dynamically trying to add legacy strictness
380$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
381    > $TESTOUT 2>&1 <<EOF
382dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
383changetype: modify
384add: olcUniqueStrict
385olcUniqueStrict: TRUE
386EOF
387RC=$?
388if test $RC != 80 ; then
389	echo "legacy strictness allowed with URIs"
390	test $KILLSERVERS != no && kill -HUP $KILLPIDS
391	exit -1
392fi
393
394#echo ----------------------
395echo Dynamically trying a bad filter...
396$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
397    > $TESTOUT 2>&1 <<EOF
398dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
399changetype: modify
400replace: olcUniqueURI
401olcUniqueURI: ldap:///?sn?sub?((cn=e*))
402EOF
403RC=$?
404if test $RC != 80 ; then
405	echo "bad filter allowed"
406	test $KILLSERVERS != no && kill -HUP $KILLPIDS
407	exit -1
408fi
409
410echo Verifying second configuration intact...
411$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/second-config-recheck.ldif
412diff $TESTDIR/second-config-recheck.ldif $TESTDIR/second-reference.ldif > /dev/null 2>&1
413RC=$?
414if test $RC != 0 ; then
415    echo "Second configuration damaged by rejected modifies."
416    test $KILLSERVERS != no && kill -HUP $KILLPIDS
417    exit -1
418fi
419
420#echo ----------------------
421echo Dynamically reconfiguring to use different URIs...
422$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
423    > $TESTOUT 2>&1 <<EOF
424dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
425changetype: modify
426add: olcUniqueURI
427olcUniqueURI: ldap:///?sn?sub?(cn=e*)
428olcUniqueURI: ldap:///?uid?sub?(cn=edgar)
429-
430delete: olcUniqueURI
431olcUniqueURI: ldap:///?description?one
432EOF
433RC=$?
434if test $RC != 0 ; then
435	echo "unable to reconfigure"
436	test $KILLSERVERS != no && kill -HUP $KILLPIDS
437	exit -1
438fi
439
440echo Dynamically retrieving third configuration...
441$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/third-config.ldif
442cat >$TESTDIR/third-reference.ldif <<EOF
443dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
444objectClass: olcOverlayConfig
445objectClass: olcUniqueConfig
446olcOverlay: {0}unique
447olcUniqueURI: ldap:///?employeeNumber,displayName?sub
448olcUniqueURI: ldap:///?sn?sub?(cn=e*)
449olcUniqueURI: ldap:///?uid?sub?(cn=edgar)
450
451EOF
452diff $TESTDIR/third-config.ldif $TESTDIR/third-reference.ldif > /dev/null 2>&1
453RC=$?
454if test $RC != 0 ; then
455    echo "Third configuration is not reported correctly."
456    test $KILLSERVERS != no && kill -HUP $KILLPIDS
457    exit -1
458fi
459
460echo "Adding a record unique in both domains if filtered..."
461
462$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
463	 $TESTOUT 2>&1 << EOF
464dn: uid=edgar,ou=users,o=unique
465objectClass: inetOrgPerson
466uid: edgar
467sn: johnson
468cn: edgar
469EOF
470
471RC=$?
472if test $RC != 0 ; then
473	echo "unique check failed ($RC)!"
474	test $KILLSERVERS != no && kill -HUP $KILLPIDS
475	exit -1
476fi
477
478echo "Adding a record unique in all domains because of filter conditions "
479$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
480	 $TESTOUT 2>&1 << EOF
481dn: uid=empty,ou=users,o=unique
482objectClass: inetOrgPerson
483uid: edgar
484cn: empty
485sn: empty
486EOF
487
488RC=$?
489if test $RC != 0 ; then
490	echo "spurious unique error ($RC)!"
491	test $KILLSERVERS != no && kill -HUP $KILLPIDS
492	exit -1
493fi
494
495
496echo "Adding a record unique in one domain, non-unique in the filtered domain..."
497
498$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
499	 $TESTOUT 2>&1 << EOF
500dn: uid=elvis,ou=users,o=unique
501objectClass: inetOrgPerson
502uid: elvis
503sn: johnson
504cn: elvis
505EOF
506
507RC=$?
508if test $RC != 19 ; then
509	echo "unique check failed ($RC)!"
510	test $KILLSERVERS != no && kill -HUP $KILLPIDS
511	exit -1
512fi
513
514#echo ----------------------
515echo Dynamically reconfiguring to use attribute-ignore URIs...
516$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
517    > $TESTOUT 2>&1 <<EOF
518dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
519changetype: modify
520replace: olcUniqueURI
521olcUniqueURI: ignore ldap:///?objectClass,uid,cn,sn?sub
522EOF
523RC=$?
524if test $RC != 0 ; then
525	echo "unable to reconfigure"
526	test $KILLSERVERS != no && kill -HUP $KILLPIDS
527	exit -1
528fi
529
530echo Dynamically retrieving fourth configuration...
531$LDAPSEARCH -S "" -b olcOverlay='{0}'unique,olcDatabase='{1}'$BACKEND,cn=config -D cn=config -y $CONFIGPWF -h $LOCALHOST -p $PORT1 -LLL | tr -d \\r >$TESTDIR/fourth-config.ldif
532cat >$TESTDIR/fourth-reference.ldif <<EOF
533dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
534objectClass: olcOverlayConfig
535objectClass: olcUniqueConfig
536olcOverlay: {0}unique
537olcUniqueURI: ignore ldap:///?objectClass,uid,cn,sn?sub
538
539EOF
540diff $TESTDIR/fourth-config.ldif $TESTDIR/fourth-reference.ldif > /dev/null 2>&1
541RC=$?
542if test $RC != 0 ; then
543    echo "Fourth configuration is not reported correctly."
544    test $KILLSERVERS != no && kill -HUP $KILLPIDS
545    exit -1
546fi
547
548echo "Adding a record unique in the ignore-domain..."
549
550$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
551	 $TESTOUT 2>&1 << EOF
552dn: uid=elvis,ou=users,o=unique
553objectClass: inetOrgPerson
554uid: elvis
555sn: johnson
556cn: elvis
557description: left the building
558EOF
559
560RC=$?
561if test $RC != 0 ; then
562	echo "unique check failed ($RC)!"
563	test $KILLSERVERS != no && kill -HUP $KILLPIDS
564	exit -1
565fi
566
567echo "Adding a record non-unique in the ignore-domain..."
568
569$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
570	 $TESTOUT 2>&1 << EOF
571dn: uid=harry,ou=users,o=unique
572objectClass: inetOrgPerson
573uid: harry
574sn: johnson
575cn: harry
576description: left the building
577EOF
578
579RC=$?
580if test $RC != 19 ; then
581	echo "unique check failed ($RC)!"
582	test $KILLSERVERS != no && kill -HUP $KILLPIDS
583	exit -1
584fi
585
586test $KILLSERVERS != no && kill -HUP $KILLPIDS
587
588echo ">>>>> Test succeeded"
589
590test $KILLSERVERS != no && wait
591
592exit 0
593