2 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that this notice is preserved and that due credit is given
11 * to the University of Michigan at Ann Arbor. The name of the University
12 * may not be used to endorse or promote products derived from this
13 * software without specific prior written permission. This software
14 * is provided ``as is'' without express or implied warranty.
22 modify_check_duplicates(
23 AttributeDescription *ad,
29 char *textbuf, size_t textlen )
31 int i, j, numvals = 0, nummods,
32 rc = LDAP_SUCCESS, matched;
34 /* needs major reworking */
36 BerVarray nvals = NULL, nmods = NULL;
39 * FIXME: better do the following
41 * - count the existing values
42 * - count the new values
44 * - if the existing values are less than the new ones {
45 * - normalize all the existing values
46 * - for each new value {
48 * - check with existing
49 * - cross-check with already normalized new vals
52 * - for each new value {
54 * - cross-check with already normalized new vals
56 * - for each existing value {
58 * - check with already normalized new values
62 * The first case is good when adding a lot of new values,
63 * and significantly at first import of values (e.g. adding
64 * a new group); the latter case seems to be quite important
65 * as well, because it is likely to be the most frequently
66 * used when administering the entry. The current
67 * implementation will always normalize all the existing
68 * values before checking. If there's no duplicate, the
69 * performances should not change; they will in case of error.
72 for ( nummods = 0; mods[ nummods ].bv_val != NULL; nummods++ )
73 /* count new values */ ;
76 for ( numvals = 0; vals[ numvals ].bv_val != NULL; numvals++ )
77 /* count existing values */ ;
79 if ( numvals < nummods ) {
80 nvals = SLAP_CALLOC( numvals + 1, sizeof( struct berval ) );
83 LDAP_LOG( OPERATION, ERR,
84 "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
86 Debug( LDAP_DEBUG_ANY,
87 "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
92 /* normalize the existing values first */
93 for ( j = 0; vals[ j ].bv_val != NULL; j++ ) {
94 rc = value_normalize( ad, SLAP_MR_EQUALITY,
95 &vals[ j ], &nvals[ j ], text );
97 /* existing attribute values must normalize */
98 assert( rc == LDAP_SUCCESS );
100 if ( rc != LDAP_SUCCESS ) {
101 nvals[ j ].bv_val = NULL;
105 nvals[ j ].bv_val = NULL;
110 * If the existing values are less than the new values,
111 * it is more convenient to normalize all the existing
112 * values and test each new value against them first,
113 * then to other already normalized values
115 nmods = SLAP_CALLOC( nummods + 1, sizeof( struct berval ) );
116 if ( nmods == NULL ) {
118 LDAP_LOG( OPERATION, ERR,
119 "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
121 Debug( LDAP_DEBUG_ANY,
122 "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
127 for ( i=0; mods[i].bv_val != NULL; i++ ) {
128 rc = value_normalize( ad, SLAP_MR_EQUALITY,
129 &mods[i], &nmods[i], text );
131 if ( rc != LDAP_SUCCESS ) {
132 nmods[i].bv_val = NULL;
136 if ( numvals > 0 && numvals < nummods ) {
137 for ( matched=0, j=0; nvals[j].bv_val; j++ ) {
140 rc = (*mr->smr_match)( &match,
141 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
142 ad->ad_type->sat_syntax,
143 mr, &nmods[ i ], &nvals[ j ] );
145 if ( rc != LDAP_SUCCESS ) {
146 nmods[ i + 1 ].bv_val = NULL;
148 snprintf( textbuf, textlen,
149 "%s: matching rule failed",
150 ad->ad_cname.bv_val );
160 snprintf( textbuf, textlen,
161 "%s: value #%d provided more than once",
162 ad->ad_cname.bv_val, i );
163 rc = LDAP_TYPE_OR_VALUE_EXISTS;
164 nmods[ i + 1 ].bv_val = NULL;
169 if ( permissive && matched == j ) {
170 nmods[ i + 1 ].bv_val = NULL;
171 rc = LDAP_TYPE_OR_VALUE_EXISTS;
176 for ( matched = 0, j = 0; j < i; j++ ) {
179 rc = (*mr->smr_match)( &match,
180 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
181 ad->ad_type->sat_syntax,
182 mr, &nmods[ i ], &nmods[ j ] );
183 if ( rc != LDAP_SUCCESS ) {
184 nmods[ i + 1 ].bv_val = NULL;
186 snprintf( textbuf, textlen,
187 "%s: matching rule failed",
188 ad->ad_cname.bv_val );
198 snprintf( textbuf, textlen,
199 "%s: value #%d provided more than once",
200 ad->ad_cname.bv_val, j );
201 rc = LDAP_TYPE_OR_VALUE_EXISTS;
202 nmods[ i + 1 ].bv_val = NULL;
207 if ( permissive && matched == j ) {
208 nmods[ i + 1 ].bv_val = NULL;
209 rc = LDAP_TYPE_OR_VALUE_EXISTS;
213 nmods[ i ].bv_val = NULL;
216 * if new values are more than existing values, it is more
217 * convenient to normalize and check all new values first,
218 * then check each new value against existing values, which
219 * can be normalized in place
222 if ( numvals >= nummods ) {
223 for ( j = 0; vals[ j ].bv_val; j++ ) {
224 struct berval asserted;
226 rc = value_normalize( ad, SLAP_MR_EQUALITY,
227 &vals[ j ], &asserted, text );
229 if ( rc != LDAP_SUCCESS ) {
233 for ( matched = 0, i = 0; nmods[ i ].bv_val; i++ ) {
236 rc = (*mr->smr_match)( &match,
237 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
238 ad->ad_type->sat_syntax,
239 mr, &nmods[ i ], &asserted );
240 if ( rc != LDAP_SUCCESS ) {
242 snprintf( textbuf, textlen,
243 "%s: matching rule failed",
244 ad->ad_cname.bv_val );
254 snprintf( textbuf, textlen,
255 "%s: value #%d provided more than once",
256 ad->ad_cname.bv_val, j );
257 rc = LDAP_TYPE_OR_VALUE_EXISTS;
262 if ( permissive && matched == i ) {
263 rc = LDAP_TYPE_OR_VALUE_EXISTS;
271 ber_bvarray_free( nvals );
274 ber_bvarray_free( nmods );
287 char *textbuf, size_t textlen
293 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
296 switch( mod->sm_op ) {
300 case LDAP_MOD_REPLACE:
308 a = attr_find( e->e_attrs, mod->sm_desc );
311 * With permissive set, as long as the attribute being added
312 * has the same value(s?) as the existing attribute, then the
313 * modify will succeed.
316 /* check if the values we're adding already exist */
317 if( mr == NULL || !mr->smr_match ) {
319 /* we should have no normalized values as there is no equality rule */
320 /* assert( mod->sm_nvalues[0].bv_val == NULL); */
324 /* do not allow add of additional attribute
325 if no equality rule exists */
327 snprintf( textbuf, textlen,
328 "modify/%s: %s: no equality matching rule",
329 op, mod->sm_desc->ad_cname.bv_val );
330 return LDAP_INAPPROPRIATE_MATCHING;
333 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
334 /* test asserted values against existing values */
337 /* we should have no normalized values as there
338 is no equality rule */
339 assert( a->a_nvals == NULL);
341 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
342 if ( bvmatch( &mod->sm_bvalues[i], &a->a_vals[j] ) ) {
347 /* value exists already */
349 snprintf( textbuf, textlen,
350 "modify/%s: %s: value #%i already exists",
351 op, mod->sm_desc->ad_cname.bv_val, j );
352 return LDAP_TYPE_OR_VALUE_EXISTS;
355 if ( permissive && matched == j ) {
356 /* values already exist; do nothing */
361 /* test asserted values against themselves */
362 for( j = 0; j < i; j++ ) {
363 if ( bvmatch( &mod->sm_bvalues[i],
364 &mod->sm_bvalues[j] ) ) {
366 /* value exists already */
368 snprintf( textbuf, textlen,
369 "modify/%s: %s: value #%i already exists",
370 op, mod->sm_desc->ad_cname.bv_val, j );
371 return LDAP_TYPE_OR_VALUE_EXISTS;
378 /* no normalization is done in this routine nor
379 * in the matching routines called by this routine.
380 * values are now normalized once on input to the
381 * server (whether from LDAP or from the underlying
383 * This should outperform the old code. No numbers
388 * The original code performs ( n ) normalizations
389 * and ( n * ( n - 1 ) / 2 ) matches, which hide
390 * the same number of normalizations. The new code
391 * performs the same number of normalizations ( n )
392 * and ( n * ( n - 1 ) / 2 ) mem compares, far less
393 * expensive than an entire match, if a match is
394 * equivalent to a normalization and a mem compare ...
396 * This is far more memory expensive than the previous,
397 * but it can heavily improve performances when big
398 * chunks of data are added (typical example is a group
399 * with thousands of DN-syntax members; on my system:
400 * for members of 5-RDN DNs,
402 members orig bvmatch (dirty) new
403 1000 0m38.456s 0m0.553s 0m0.608s
404 2000 2m33.341s 0m0.851s 0m1.003s
406 * Moreover, 100 groups with 10000 members each were
407 * added in 37m27.933s (an analogous LDIF file was
408 * loaded into Active Directory in 38m28.682s, BTW).
410 * Maybe we could switch to the new algorithm when
411 * the number of values overcomes a given threshold?
417 if ( mod->sm_bvalues[1].bv_val == 0 ) {
419 struct berval asserted;
423 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
424 &mod->sm_bvalues[ 0 ], &asserted, text );
425 if ( rc != LDAP_SUCCESS ) {
430 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
434 if( mod->sm_nvalues ) {
435 rc = value_match( &match, mod->sm_desc, mr,
437 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
438 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
439 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
445 rc = value_match( &match, mod->sm_desc, mr,
447 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
454 rc = value_match( &match, mod->sm_desc, mr,
455 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
461 if( rc == LDAP_SUCCESS && match == 0 ) {
466 free( asserted.bv_val );
468 snprintf( textbuf, textlen,
469 "modify/%s: %s: value #0 already exists",
470 op, mod->sm_desc->ad_cname.bv_val, 0 );
471 return LDAP_TYPE_OR_VALUE_EXISTS;
474 if ( permissive && matched == i ) {
475 /* values already exist; do nothing */
481 rc = modify_check_duplicates( mod->sm_desc, mr,
482 a ? a->a_vals : NULL, mod->sm_bvalues,
484 text, textbuf, textlen );
486 if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
490 if ( rc != LDAP_SUCCESS ) {
498 if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 )
500 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
503 /* this should return result of attr_merge */
505 snprintf( textbuf, textlen,
506 "modify/%s: %s: merge error",
507 op, mod->sm_desc->ad_cname.bv_val );
515 modify_delete_values(
520 char *textbuf, size_t textlen
523 int i, j, k, rc = LDAP_SUCCESS;
525 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
527 BerVarray nvals = NULL;
532 * If permissive is set, then the non-existence of an
533 * attribute is not treated as an error.
536 /* delete the entire attribute */
537 if ( mod->sm_bvalues == NULL ) {
538 rc = attr_delete( &e->e_attrs, mod->sm_desc );
542 } else if( rc != LDAP_SUCCESS ) {
544 snprintf( textbuf, textlen,
545 "modify/delete: %s: no such attribute",
546 mod->sm_desc->ad_cname.bv_val );
547 rc = LDAP_NO_SUCH_ATTRIBUTE;
552 if( mr == NULL || !mr->smr_match ) {
553 /* disallow specific attributes from being deleted if
556 snprintf( textbuf, textlen,
557 "modify/delete: %s: no equality matching rule",
558 mod->sm_desc->ad_cname.bv_val );
559 return LDAP_INAPPROPRIATE_MATCHING;
562 /* delete specific values - find the attribute first */
563 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
568 snprintf( textbuf, textlen,
569 "modify/delete: %s: no such attribute",
570 mod->sm_desc->ad_cname.bv_val );
571 return LDAP_NO_SUCH_ATTRIBUTE;
575 /* find each value to delete */
576 for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ )
577 /* count existing values */ ;
579 nvals = (BerVarray)SLAP_CALLOC( j + 1, sizeof ( struct berval ) );
580 if( nvals == NULL ) {
582 LDAP_LOG( OPERATION, ERR,
583 "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
585 Debug( LDAP_DEBUG_ANY,
586 "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
591 /* normalize existing values */
592 for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ ) {
593 rc = value_normalize( a->a_desc, SLAP_MR_EQUALITY,
594 &a->a_vals[ j ], &nvals[ j ], text );
596 if ( rc != LDAP_SUCCESS ) {
597 nvals[ j ].bv_val = NULL;
603 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
606 struct berval asserted;
608 /* normalize the value to be deleted */
609 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
610 &mod->sm_bvalues[ i ], &asserted, text );
612 if( rc != LDAP_SUCCESS ) {
617 for ( j = 0; nvals[ j ].bv_val != NULL; j++ )
619 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ )
625 if ( nvals[j].bv_val == &dummy ) {
631 if( mod->sm_nvalues ) {
632 assert( a->a_nvals );
633 rc = (*mr->smr_match)( &match,
634 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
635 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
636 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
637 a->a_desc->ad_type->sat_syntax,
639 &mod->sm_nvalues[i] );
641 assert( a->a_nvals == NULL );
642 rc = (*mr->smr_match)( &match,
643 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
644 a->a_desc->ad_type->sat_syntax,
646 &mod->sm_values[i] );
649 rc = (*mr->smr_match)( &match,
650 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
651 a->a_desc->ad_type->sat_syntax,
656 if ( rc != LDAP_SUCCESS ) {
658 free( asserted.bv_val );
661 snprintf( textbuf, textlen,
662 "%s: matching rule failed",
663 mod->sm_desc->ad_cname.bv_val );
673 /* delete value and mark it as dummy */
675 free( a->a_vals[j].bv_val );
676 a->a_vals[j].bv_val = &dummy;
678 free( a->a_nvals[j].bv_val );
679 a->a_nvals[j].bv_val = &dummy;
682 free( nvals[ j ].bv_val );
683 nvals[ j ].bv_val = &dummy;
690 free( asserted.bv_val );
695 snprintf( textbuf, textlen,
696 "modify/delete: %s: no such value",
697 mod->sm_desc->ad_cname.bv_val );
698 rc = LDAP_NO_SUCH_ATTRIBUTE;
703 /* compact array skipping dummies */
705 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ )
707 for ( k = 0, j = 0; nvals[k].bv_val != NULL; j++, k++ )
712 if( a->a_vals[k].bv_val == &dummy ) {
713 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
717 /* delete and skip dummies */ ;
718 for ( ; nvals[ k ].bv_val == &dummy; k++ ) {
719 free( a->a_vals[ k ].bv_val );
723 a->a_vals[ j ] = a->a_vals[ k ];
726 a->a_nvals[ j ] = a->a_nvals[ k ];
732 if ( a->a_vals[ k ].bv_val == NULL ) {
740 a->a_vals[j].bv_val = NULL;
742 if (a->a_nvals) a->a_nvals[j].bv_val = NULL;
745 assert( i == k - j );
748 /* if no values remain, delete the entire attribute */
749 if ( a->a_vals[0].bv_val == NULL ) {
750 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
752 snprintf( textbuf, textlen,
753 "modify/delete: %s: no such attribute",
754 mod->sm_desc->ad_cname.bv_val );
755 rc = LDAP_NO_SUCH_ATTRIBUTE;
762 /* delete the remaining normalized values */
763 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
764 if ( nvals[ j ].bv_val != &dummy ) {
765 ber_memfree( nvals[ j ].bv_val );
768 ber_memfree( nvals );
776 modify_replace_values(
781 char *textbuf, size_t textlen
784 (void) attr_delete( &e->e_attrs, mod->sm_desc );
786 if ( mod->sm_bvalues ) {
787 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
799 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
800 mod->sm_values = NULL;
803 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
804 mod->sm_nvalues = NULL;
807 if( freeit ) free( mod );
817 for ( ; ml != NULL; ml = next ) {
820 slap_mod_free( &ml->sml_mod, 0 );