xref: /netbsd-src/external/bsd/openldap/dist/tests/scripts/test024-unique (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1#! /bin/sh
2# $OpenLDAP: pkg/ldap/tests/scripts/test024-unique,v 1.8.2.5 2008/02/11 23:26:51 kurt Exp $
3## This work is part of OpenLDAP Software <http://www.openldap.org/>.
4##
5## Copyright 2004-2008 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*)
428-
429delete: olcUniqueURI
430olcUniqueURI: ldap:///?description?one
431EOF
432RC=$?
433if test $RC != 0 ; then
434	echo "unable to reconfigure"
435	test $KILLSERVERS != no && kill -HUP $KILLPIDS
436	exit -1
437fi
438
439echo Dynamically retrieving third configuration...
440$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
441cat >$TESTDIR/third-reference.ldif <<EOF
442dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
443objectClass: olcOverlayConfig
444objectClass: olcUniqueConfig
445olcOverlay: {0}unique
446olcUniqueURI: ldap:///?employeeNumber,displayName?sub
447olcUniqueURI: ldap:///?sn?sub?(cn=e*)
448
449EOF
450diff $TESTDIR/third-config.ldif $TESTDIR/third-reference.ldif > /dev/null 2>&1
451RC=$?
452if test $RC != 0 ; then
453    echo "Third configuration is not reported correctly."
454    test $KILLSERVERS != no && kill -HUP $KILLPIDS
455    exit -1
456fi
457
458echo "Adding a record unique in both domains if filtered..."
459
460$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
461	 $TESTOUT 2>&1 << EOF
462dn: uid=edgar,ou=users,o=unique
463objectClass: inetOrgPerson
464uid: edgar
465sn: johnson
466cn: edgar
467EOF
468
469RC=$?
470if test $RC != 0 ; then
471	echo "unique check failed ($RC)!"
472	test $KILLSERVERS != no && kill -HUP $KILLPIDS
473	exit -1
474fi
475
476echo "Adding a record unique in one domain, non-unique in the filtered domain..."
477
478$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
479	 $TESTOUT 2>&1 << EOF
480dn: uid=elvis,ou=users,o=unique
481objectClass: inetOrgPerson
482uid: elvis
483sn: johnson
484cn: elvis
485EOF
486
487RC=$?
488if test $RC != 19 ; then
489	echo "unique check failed ($RC)!"
490	test $KILLSERVERS != no && kill -HUP $KILLPIDS
491	exit -1
492fi
493
494#echo ----------------------
495echo Dynamically reconfiguring to use attribute-ignore URIs...
496$LDAPMODIFY -D cn=config -h $LOCALHOST -p $PORT1 -y $CONFIGPWF \
497    > $TESTOUT 2>&1 <<EOF
498dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
499changetype: modify
500replace: olcUniqueURI
501olcUniqueURI: ignore ldap:///?objectClass,uid,cn,sn?sub
502EOF
503RC=$?
504if test $RC != 0 ; then
505	echo "unable to reconfigure"
506	test $KILLSERVERS != no && kill -HUP $KILLPIDS
507	exit -1
508fi
509
510echo Dynamically retrieving fourth configuration...
511$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
512cat >$TESTDIR/fourth-reference.ldif <<EOF
513dn: olcOverlay={0}unique,olcDatabase={1}$BACKEND,cn=config
514objectClass: olcOverlayConfig
515objectClass: olcUniqueConfig
516olcOverlay: {0}unique
517olcUniqueURI: ignore ldap:///?objectClass,uid,cn,sn?sub
518
519EOF
520diff $TESTDIR/fourth-config.ldif $TESTDIR/fourth-reference.ldif > /dev/null 2>&1
521RC=$?
522if test $RC != 0 ; then
523    echo "Fourth configuration is not reported correctly."
524    test $KILLSERVERS != no && kill -HUP $KILLPIDS
525    exit -1
526fi
527
528echo "Adding a record unique in the ignore-domain..."
529
530$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
531	 $TESTOUT 2>&1 << EOF
532dn: uid=elvis,ou=users,o=unique
533objectClass: inetOrgPerson
534uid: elvis
535sn: johnson
536cn: elvis
537description: left the building
538EOF
539
540RC=$?
541if test $RC != 0 ; then
542	echo "unique check failed ($RC)!"
543	test $KILLSERVERS != no && kill -HUP $KILLPIDS
544	exit -1
545fi
546
547echo "Adding a record non-unique in the ignore-domain..."
548
549$LDAPADD -D "$UNIQUEDN" -h $LOCALHOST -p $PORT1 -w $PASSWD > \
550	 $TESTOUT 2>&1 << EOF
551dn: uid=harry,ou=users,o=unique
552objectClass: inetOrgPerson
553uid: harry
554sn: johnson
555cn: harry
556description: left the building
557EOF
558
559RC=$?
560if test $RC != 19 ; then
561	echo "unique check failed ($RC)!"
562	test $KILLSERVERS != no && kill -HUP $KILLPIDS
563	exit -1
564fi
565
566test $KILLSERVERS != no && kill -HUP $KILLPIDS
567
568echo ">>>>> Test succeeded"
569
570test $KILLSERVERS != no && wait
571
572exit 0
573