]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
cedc658c2023eed54db9d92ef30660ca45cda6f3
[openldap] / servers / slapd / mods.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * Copyright (c) 1995 Regents of the University of Michigan.
7  * All rights reserved.
8  *
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.
15  */
16
17 #include "portable.h"
18
19 #include "slap.h"
20
21 int
22 modify_check_duplicates(
23         AttributeDescription    *ad,
24         MatchingRule            *mr,
25         BerVarray               vals,
26         BerVarray               mods,
27         int                     permissive,
28         const char      **text,
29         char *textbuf, size_t textlen )
30 {
31         int             i, j, numvals = 0, nummods,
32                         rc = LDAP_SUCCESS, matched;
33 #ifdef SLAP_NVALUES
34         /* needs major reworking */
35 #else
36         BerVarray       nvals = NULL, nmods = NULL;
37
38         /*
39          * FIXME: better do the following
40          * 
41          *   - count the existing values
42          *   - count the new values
43          *   
44          *   - if the existing values are less than the new ones {
45          *       - normalize all the existing values
46          *       - for each new value {
47          *           - normalize
48          *           - check with existing
49          *           - cross-check with already normalized new vals
50          *       }
51          *   } else {
52          *       - for each new value {
53          *           - normalize
54          *           - cross-check with already normalized new vals
55          *       }
56          *       - for each existing value {
57          *           - normalize
58          *           - check with already normalized new values
59          *       }
60          *   }
61          *
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.
70          */
71
72         for ( nummods = 0; mods[ nummods ].bv_val != NULL; nummods++ )
73                 /* count new values */ ;
74
75         if ( vals ) {
76                 for ( numvals = 0; vals[ numvals ].bv_val != NULL; numvals++ )
77                         /* count existing values */ ;
78
79                 if ( numvals < nummods ) {
80                         nvals = SLAP_CALLOC( numvals + 1, sizeof( struct berval ) );
81                         if( nvals == NULL ) {
82 #ifdef NEW_LOGGING
83                                 LDAP_LOG( OPERATION, ERR,
84                                         "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
85 #else
86                                 Debug( LDAP_DEBUG_ANY, 
87                                         "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
88 #endif
89                                 goto return_results;
90                         }
91
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 );
96
97                                 /* existing attribute values must normalize */
98                                 assert( rc == LDAP_SUCCESS );
99
100                                 if ( rc != LDAP_SUCCESS ) {
101                                         nvals[ j ].bv_val = NULL;
102                                         goto return_results;
103                                 }
104                         }
105                         nvals[ j ].bv_val = NULL;
106                 }
107         }
108
109         /*
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
114          */
115         nmods = SLAP_CALLOC( nummods + 1, sizeof( struct berval ) );
116         if ( nmods == NULL ) {
117 #ifdef NEW_LOGGING
118                 LDAP_LOG( OPERATION, ERR,
119                         "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
120 #else
121                 Debug( LDAP_DEBUG_ANY, 
122                         "modify_check_duplicates: SLAP_CALLOC failed", 0, 0, 0 );
123 #endif
124                 goto return_results;
125         }
126
127         for ( i=0; mods[i].bv_val != NULL; i++ ) {
128                 rc = value_normalize( ad, SLAP_MR_EQUALITY,
129                         &mods[i], &nmods[i], text );
130
131                 if ( rc != LDAP_SUCCESS ) {
132                         nmods[i].bv_val = NULL;
133                         goto return_results;
134                 }
135
136                 if ( numvals > 0 && numvals < nummods ) {
137                         for ( matched=0, j=0; nvals[j].bv_val; j++ ) {
138                                 int match;
139
140                                 rc = (*mr->smr_match)( &match,
141                                         SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
142                                         ad->ad_type->sat_syntax,
143                                         mr, &nmods[ i ], &nvals[ j ] );
144
145                                 if ( rc != LDAP_SUCCESS ) {
146                                         nmods[ i + 1 ].bv_val = NULL;
147                                         *text = textbuf;
148                                         snprintf( textbuf, textlen,
149                                                 "%s: matching rule failed",
150                                                 ad->ad_cname.bv_val );
151                                         goto return_results;
152                                 }
153
154                                 if ( match == 0 ) {
155                                         if ( permissive ) {
156                                                 matched++;
157                                                 continue;
158                                         }
159                                         *text = textbuf;
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;
165                                         goto return_results;
166                                 }
167                         }
168
169                         if ( permissive && matched == j ) {
170                                 nmods[ i + 1 ].bv_val = NULL;
171                                 rc = LDAP_TYPE_OR_VALUE_EXISTS;
172                                 goto return_results;
173                         }
174                 }
175         
176                 for ( matched = 0, j = 0; j < i; j++ ) {
177                         int match;
178
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;
185                                 *text = textbuf;
186                                 snprintf( textbuf, textlen,
187                                         "%s: matching rule failed",
188                                         ad->ad_cname.bv_val );
189                                 goto return_results;
190                         }
191
192                         if ( match == 0 ) {
193                                 if ( permissive ) {
194                                         matched++;
195                                         continue;
196                                 }
197                                 *text = textbuf;
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;
203                                 goto return_results;
204                         }
205                 }
206
207                 if ( permissive && matched == j ) {
208                         nmods[ i + 1 ].bv_val = NULL;
209                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
210                         goto return_results;
211                 }
212         }
213         nmods[ i ].bv_val = NULL;
214
215         /*
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
220          */
221
222         if ( numvals >= nummods ) {
223                 for ( j = 0; vals[ j ].bv_val; j++ ) {
224                         struct berval   asserted;
225
226                         rc = value_normalize( ad, SLAP_MR_EQUALITY,
227                                 &vals[ j ], &asserted, text );
228
229                         if ( rc != LDAP_SUCCESS ) {
230                                 goto return_results;
231                         }
232
233                         for ( matched = 0, i = 0; nmods[ i ].bv_val; i++ ) {
234                                 int match;
235
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 ) {
241                                         *text = textbuf;
242                                         snprintf( textbuf, textlen,
243                                                 "%s: matching rule failed",
244                                                 ad->ad_cname.bv_val );
245                                         goto return_results;
246                                 }
247
248                                 if ( match == 0 ) {
249                                         if ( permissive ) {
250                                                 matched++;
251                                                 continue;
252                                         }
253                                         *text = textbuf;
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;
258                                         goto return_results;
259                                 }
260                         }
261
262                         if ( permissive && matched == i ) {
263                                 rc = LDAP_TYPE_OR_VALUE_EXISTS;
264                                 goto return_results;
265                         }
266                 }
267         }
268
269 return_results:;
270         if ( nvals ) {
271                 ber_bvarray_free( nvals );
272         }
273         if ( nmods ) {
274                 ber_bvarray_free( nmods );
275         }
276
277 #endif
278         return rc;
279 }
280
281 int
282 modify_add_values(
283         Entry   *e,
284         Modification    *mod,
285         int     permissive,
286         const char      **text,
287         char *textbuf, size_t textlen
288 )
289 {
290         int             i, j;
291         int             matched;
292         Attribute       *a;
293         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
294         const char *op;
295
296         switch( mod->sm_op ) {
297         case LDAP_MOD_ADD:
298                 op = "add";
299                 break;
300         case LDAP_MOD_REPLACE:
301                 op = "replace";
302                 break;
303         default:
304                 op = "?";
305                 assert( 0 );
306         }
307
308         a = attr_find( e->e_attrs, mod->sm_desc );
309
310         /*
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.
314          */
315
316         /* check if the values we're adding already exist */
317         if( mr == NULL || !mr->smr_match ) {
318 #ifdef SLAP_NVALUES
319                 /* we should have no normalized values as there is no equality rule */
320                 /* assert( mod->sm_nvalues[0].bv_val == NULL); */
321 #endif
322
323                 if ( a != NULL ) {
324                         /* do not allow add of additional attribute
325                                 if no equality rule exists */
326                         *text = textbuf;
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;
331                 }
332
333                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
334                         /* test asserted values against existing values */
335                         if( a ) {
336 #ifdef SLAP_NVALUES
337                                 /* we should have no normalized values as there
338                                         is no equality rule */
339                                 assert( a->a_nvals == NULL);
340 #endif
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] ) ) {
343                                                 if ( permissive ) {
344                                                         matched++;
345                                                         continue;
346                                                 }
347                                                 /* value exists already */
348                                                 *text = textbuf;
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;
353                                         }
354                                 }
355                                 if ( permissive && matched == j ) {
356                                         /* values already exist; do nothing */
357                                         return LDAP_SUCCESS;
358                                 }
359                         }
360
361                         /* test asserted values against themselves */
362                         for( j = 0; j < i; j++ ) {
363                                 if ( bvmatch( &mod->sm_bvalues[i], &mod->sm_bvalues[j] ) ) {
364                                         /* value exists already */
365                                         *text = textbuf;
366                                         snprintf( textbuf, textlen,
367                                                 "modify/%s: %s: value #%i already exists",
368                                                 op, mod->sm_desc->ad_cname.bv_val, j );
369                                         return LDAP_TYPE_OR_VALUE_EXISTS;
370                                 }
371                         }
372                 }
373
374         } else {
375 #ifdef SLAP_NVALUES
376                 /* no normalization is done in this routine nor
377                  * in the matching routines called by this routine. 
378                  * values are now normalized once on input to the
379                  * server (whether from LDAP or from the underlying
380                  * database).
381                  * This should outperform the old code.  No numbers
382                  * are available yet.
383                  */
384 #else
385                 /*
386                  * The original code performs ( n ) normalizations 
387                  * and ( n * ( n - 1 ) / 2 ) matches, which hide
388                  * the same number of normalizations.  The new code
389                  * performs the same number of normalizations ( n )
390                  * and ( n * ( n - 1 ) / 2 ) mem compares, far less
391                  * expensive than an entire match, if a match is
392                  * equivalent to a normalization and a mem compare ...
393                  * 
394                  * This is far more memory expensive than the previous,
395                  * but it can heavily improve performances when big
396                  * chunks of data are added (typical example is a group
397                  * with thousands of DN-syntax members; on my system:
398                  * for members of 5-RDN DNs,
399
400                 members         orig            bvmatch (dirty) new
401                 1000            0m38.456s       0m0.553s        0m0.608s
402                 2000            2m33.341s       0m0.851s        0m1.003s
403
404                  * Moreover, 100 groups with 10000 members each were
405                  * added in 37m27.933s (an analogous LDIF file was
406                  * loaded into Active Directory in 38m28.682s, BTW).
407                  * 
408                  * Maybe we could switch to the new algorithm when
409                  * the number of values overcomes a given threshold?
410                  */
411 #endif
412
413                 int             rc;
414
415                 if ( mod->sm_bvalues[1].bv_val == 0 ) {
416                         if ( a != NULL ) {
417                                 int             i;
418 #ifndef SLAP_NVALUES
419                                 struct berval   asserted;
420                                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
421                                         &mod->sm_bvalues[ 0 ], &asserted, text );
422                                 if ( rc != LDAP_SUCCESS ) {
423                                         return rc;
424                                 }
425 #endif
426
427                                 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
428                                         int     match;
429
430 #ifdef SLAP_NVALUES
431                                         if( mod->sm_nvalues ) {
432                                                 rc = value_match( &match, mod->sm_desc, mr,
433                                                         SLAP_MR_EQUALITY
434                                                                 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
435                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
436                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
437                                                         &a->a_nvals[i],
438                                                         &mod->sm_nvalues[0],
439                                                         text );
440
441                                         } else {
442                                                 rc = value_match( &match, mod->sm_desc, mr,
443                                                         SLAP_MR_EQUALITY
444                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
445                                                         &a->a_vals[i],
446                                                         &mod->sm_values[0],
447                                                         text );
448                                         }
449
450 #else
451                                         rc = value_match( &match, mod->sm_desc, mr,
452                                                 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
453                                                 &a->a_vals[i],
454                                                 &asserted,
455                                                 text );
456 #endif
457
458                                         if( rc == LDAP_SUCCESS && match == 0 ) {
459                                                 if ( permissive ) {
460                                                         matched++;
461                                                         continue;
462                                                 }
463 #ifndef SLAP_NVALUES
464                                                 free( asserted.bv_val );
465 #endif
466                                                 *text = textbuf;
467                                                 snprintf( textbuf, textlen,
468                                                         "modify/%s: %s: value #0 already exists",
469                                                         op, mod->sm_desc->ad_cname.bv_val, 0 );
470                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
471                                         }
472                                 }
473 #ifndef SLAP_NVALUES
474                                 free( asserted.bv_val );
475 #endif
476                                 if ( permissive && matched == i ) {
477                                         /* values already exist; do nothing */
478                                         return LDAP_SUCCESS;
479                                 }
480                         }
481
482                 } else {
483                         rc = modify_check_duplicates( mod->sm_desc, mr,
484                                         a ? a->a_vals : NULL, mod->sm_bvalues,
485                                         permissive, text, textbuf, textlen );
486
487                         if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
488                                 return LDAP_SUCCESS;
489                         }
490
491                         if ( rc != LDAP_SUCCESS ) {
492                                 return rc;
493                         }
494                 }
495         }
496
497         /* no - add them */
498 #ifdef SLAP_NVALUES
499         if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 )
500 #else
501         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
502 #endif
503         {
504                 /* this should return result of attr_merge */
505                 *text = textbuf;
506                 snprintf( textbuf, textlen,
507                         "modify/%s: %s: merge error",
508                         op, mod->sm_desc->ad_cname.bv_val );
509                 return LDAP_OTHER;
510         }
511
512         return LDAP_SUCCESS;
513 }
514
515 int
516 modify_delete_values(
517         Entry   *e,
518         Modification    *mod,
519         int     permissive,
520         const char      **text,
521         char *textbuf, size_t textlen
522 )
523 {
524         int             i, j, k, rc = LDAP_SUCCESS;
525         Attribute       *a;
526         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
527 #ifndef SLAP_NVALUES
528         BerVarray       nvals = NULL;
529 #endif
530         char            dummy = '\0';
531
532         /*
533          * If permissive is set, then the non-existence of an 
534          * attribute is not treated as an error.
535          */
536
537         /* delete the entire attribute */
538         if ( mod->sm_bvalues == NULL ) {
539                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
540
541                 if( permissive ) {
542                         rc = LDAP_SUCCESS;
543                 } else if( rc != LDAP_SUCCESS ) {
544                         *text = textbuf;
545                         snprintf( textbuf, textlen,
546                                 "modify/delete: %s: no such attribute",
547                                 mod->sm_desc->ad_cname.bv_val );
548                         rc = LDAP_NO_SUCH_ATTRIBUTE;
549                 }
550                 return rc;
551         }
552
553         if( mr == NULL || !mr->smr_match ) {
554                 /* disallow specific attributes from being deleted if
555                         no equality rule */
556                 *text = textbuf;
557                 snprintf( textbuf, textlen,
558                         "modify/delete: %s: no equality matching rule",
559                         mod->sm_desc->ad_cname.bv_val );
560                 return LDAP_INAPPROPRIATE_MATCHING;
561         }
562
563         /* delete specific values - find the attribute first */
564         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
565                 if( permissive ) {
566                         return LDAP_SUCCESS;
567                 }
568                 *text = textbuf;
569                 snprintf( textbuf, textlen,
570                         "modify/delete: %s: no such attribute",
571                         mod->sm_desc->ad_cname.bv_val );
572                 return LDAP_NO_SUCH_ATTRIBUTE;
573         }
574
575 #ifndef SLAP_NVALUES
576         /* find each value to delete */
577         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ )
578                 /* count existing values */ ;
579
580         nvals = (BerVarray)SLAP_CALLOC( j + 1, sizeof ( struct berval ) );
581         if( nvals == NULL ) {
582 #ifdef NEW_LOGGING
583                 LDAP_LOG( OPERATION, ERR,
584                         "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
585 #else
586                 Debug( LDAP_DEBUG_ANY, 
587                         "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
588 #endif
589                                 goto return_results;
590         }
591
592         /* normalize existing values */
593         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ ) {
594                 rc = value_normalize( a->a_desc, SLAP_MR_EQUALITY,
595                         &a->a_vals[ j ], &nvals[ j ], text );
596
597                 if ( rc != LDAP_SUCCESS ) {
598                         nvals[ j ].bv_val = NULL;
599                         goto return_results;
600                 }
601         }
602 #endif
603
604         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
605                 int     found = 0;
606 #ifndef SLAP_NVALUES
607                 struct  berval asserted;
608
609                 /* normalize the value to be deleted */
610                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
611                         &mod->sm_bvalues[ i ], &asserted, text );
612
613                 if( rc != LDAP_SUCCESS ) {
614                         goto return_results;
615                 }
616
617                 /* search it */
618                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ )
619 #else
620                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ )
621 #endif
622                 {
623                         int match;
624
625 #ifndef SLAP_NVALUES
626                         if ( nvals[j].bv_val == &dummy ) {
627                                 continue;
628                         }
629
630 #endif
631 #ifdef SLAP_NVALUES
632                         if( mod->sm_nvalues ) {
633                                 assert( a->a_nvals );
634                                 rc = (*mr->smr_match)( &match,
635                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
636                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
637                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
638                                         a->a_desc->ad_type->sat_syntax,
639                                         mr, &a->a_nvals[j],
640                                         &mod->sm_nvalues[i] );
641                         } else {
642                                 assert( a->a_nvals == NULL );
643                                 rc = (*mr->smr_match)( &match,
644                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
645                                         a->a_desc->ad_type->sat_syntax,
646                                         mr, &a->a_nvals[j],
647                                         &mod->sm_values[i] );
648                         }
649 #else
650                         rc = (*mr->smr_match)( &match,
651                                 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
652                                 a->a_desc->ad_type->sat_syntax,
653                                 mr, &nvals[ j ],
654                                 &asserted );
655 #endif
656
657                         if ( rc != LDAP_SUCCESS ) {
658 #ifndef SLAP_NVALUES
659                                 free( asserted.bv_val );
660 #endif
661                                 *text = textbuf;
662                                 snprintf( textbuf, textlen,
663                                         "%s: matching rule failed",
664                                         mod->sm_desc->ad_cname.bv_val );
665                                 goto return_results;
666                         }
667
668                         if ( match != 0 ) {
669                                 continue;
670                         }
671
672                         found = 1;
673
674                         /* delete value and mark it as dummy */
675 #ifdef SLAP_NVALUES
676                         free( a->a_vals[j].bv_val );
677                         a->a_vals[j].bv_val = &dummy;
678                         if( a->a_nvals ) {
679                                 free( a->a_nvals[j].bv_val );
680                                 a->a_nvals[j].bv_val = &dummy;
681                         }
682 #else
683                         free( nvals[ j ].bv_val );
684                         nvals[ j ].bv_val = &dummy;
685 #endif
686
687                         break;
688                 }
689
690 #ifndef SLAP_NVALUES
691                 free( asserted.bv_val );
692 #endif
693
694                 if ( found == 0 ) {
695                         *text = textbuf;
696                         snprintf( textbuf, textlen,
697                                 "modify/delete: %s: no such value",
698                                 mod->sm_desc->ad_cname.bv_val );
699                         rc = LDAP_NO_SUCH_ATTRIBUTE;
700                         goto return_results;
701                 }
702         }
703
704         /* compact array skipping dummies */
705 #ifdef SLAP_NVALUES
706         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ )
707 #else
708         for ( k = 0, j = 0; nvals[k].bv_val != NULL; j++, k++ )
709 #endif
710         {
711 #ifdef SLAP_NVALUES
712                 /* skip dummies */
713                 if( a->a_vals[k].bv_val == &dummy ) {
714                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
715                         continue;
716                 }
717 #else
718                 /* delete and skip dummies */ ;
719                 for ( ; nvals[ k ].bv_val == &dummy; k++ ) {
720                         free( a->a_vals[ k ].bv_val );
721                 }
722 #endif
723                 if ( j != k ) {
724                         a->a_vals[ j ] = a->a_vals[ k ];
725 #ifdef SLAP_NVALUES
726                         if (a->a_nvals) {
727                                 a->a_nvals[ j ] = a->a_nvals[ k ];
728                         }
729 #endif
730                 }
731
732 #ifndef SLAP_NVALUES
733                 if ( a->a_vals[ k ].bv_val == NULL ) {
734                         break;
735                 }
736 #else
737                 j++;
738 #endif
739         }
740
741         a->a_vals[j].bv_val = NULL;
742 #ifdef SLAP_NVALUES
743         if (a->a_nvals) a->a_nvals[j].bv_val = NULL;
744 #else
745
746         assert( i == k - j );
747 #endif
748
749         /* if no values remain, delete the entire attribute */
750         if ( a->a_vals[0].bv_val == NULL ) {
751                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
752                         *text = textbuf;
753                         snprintf( textbuf, textlen,
754                                 "modify/delete: %s: no such attribute",
755                                 mod->sm_desc->ad_cname.bv_val );
756                         rc = LDAP_NO_SUCH_ATTRIBUTE;
757                 }
758         }
759
760 return_results:;
761 #ifndef SLAP_NVALUES
762         if ( nvals ) {
763                 /* delete the remaining normalized values */
764                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
765                         if ( nvals[ j ].bv_val != &dummy ) {
766                                 ber_memfree( nvals[ j ].bv_val );
767                         }
768                 }
769                 ber_memfree( nvals );
770         }
771 #endif
772
773         return rc;
774 }
775
776 int
777 modify_replace_values(
778         Entry   *e,
779         Modification    *mod,
780         int             permissive,
781         const char      **text,
782         char *textbuf, size_t textlen
783 )
784 {
785         (void) attr_delete( &e->e_attrs, mod->sm_desc );
786
787         if ( mod->sm_bvalues ) {
788                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
789         }
790
791         return LDAP_SUCCESS;
792 }
793
794 void
795 slap_mod_free(
796         Modification    *mod,
797         int                             freeit
798 )
799 {
800         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
801         mod->sm_values = NULL;
802
803 #ifdef SLAP_NVALUES
804         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
805         mod->sm_nvalues = NULL;
806 #endif
807
808         if( freeit ) free( mod );
809 }
810
811 void
812 slap_mods_free(
813     Modifications       *ml
814 )
815 {
816         Modifications *next;
817
818         for ( ; ml != NULL; ml = next ) {
819                 next = ml->sml_next;
820
821                 slap_mod_free( &ml->sml_mod, 0 );
822                 free( ml );
823         }
824 }
825