]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
fe1cd48aa880e4527937b52dda31d2001ca3273f
[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         /* this function is no longer used */
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                 if ( a != NULL ) {
319                         /* do not allow add of additional attribute
320                                 if no equality rule exists */
321                         *text = textbuf;
322                         snprintf( textbuf, textlen,
323                                 "modify/%s: %s: no equality matching rule",
324                                 op, mod->sm_desc->ad_cname.bv_val );
325                         return LDAP_INAPPROPRIATE_MATCHING;
326                 }
327
328                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
329                         /* test asserted values against existing values */
330                         if( a ) {
331                                 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
332                                         if ( bvmatch( &mod->sm_bvalues[i], &a->a_vals[j] ) ) {
333                                                 if ( permissive ) {
334                                                         matched++;
335                                                         continue;
336                                                 }
337                                                 /* value exists already */
338                                                 *text = textbuf;
339                                                 snprintf( textbuf, textlen,
340                                                         "modify/%s: %s: value #%i already exists",
341                                                         op, mod->sm_desc->ad_cname.bv_val, j );
342                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
343                                         }
344                                 }
345                                 if ( permissive && matched == j ) {
346                                         /* values already exist; do nothing */
347                                         return LDAP_SUCCESS;
348                                 }
349                         }
350
351                         /* test asserted values against themselves */
352                         for( j = 0; j < i; j++ ) {
353                                 if ( bvmatch( &mod->sm_bvalues[i], &mod->sm_bvalues[j] ) ) {
354                                         /* value exists already */
355                                         *text = textbuf;
356                                         snprintf( textbuf, textlen,
357                                                 "modify/%s: %s: value #%i already exists",
358                                                 op, mod->sm_desc->ad_cname.bv_val, j );
359                                         return LDAP_TYPE_OR_VALUE_EXISTS;
360                                 }
361                         }
362                 }
363
364         } else {
365 #ifdef SLAP_NVALUES
366                 /* no normalization is done in this routine nor
367                  * in the matching routines called by this routine. 
368                  * values are now normalized once on input to the
369                  * server (whether from LDAP or from the underlying
370                  * database).
371                  * This should outperform the old code.  No numbers
372                  * are available yet.
373                  */
374 #else
375                 /*
376                  * The original code performs ( n ) normalizations 
377                  * and ( n * ( n - 1 ) / 2 ) matches, which hide
378                  * the same number of normalizations.  The new code
379                  * performs the same number of normalizations ( n )
380                  * and ( n * ( n - 1 ) / 2 ) mem compares, far less
381                  * expensive than an entire match, if a match is
382                  * equivalent to a normalization and a mem compare ...
383                  * 
384                  * This is far more memory expensive than the previous,
385                  * but it can heavily improve performances when big
386                  * chunks of data are added (typical example is a group
387                  * with thousands of DN-syntax members; on my system:
388                  * for members of 5-RDN DNs,
389
390                 members         orig            bvmatch (dirty) new
391                 1000            0m38.456s       0m0.553s        0m0.608s
392                 2000            2m33.341s       0m0.851s        0m1.003s
393
394                  * Moreover, 100 groups with 10000 members each were
395                  * added in 37m27.933s (an analogous LDIF file was
396                  * loaded into Active Directory in 38m28.682s, BTW).
397                  * 
398                  * Maybe we could switch to the new algorithm when
399                  * the number of values overcomes a given threshold?
400                  */
401 #endif
402
403                 int             rc;
404
405                 if ( mod->sm_bvalues[1].bv_val == 0 ) {
406                         if ( a != NULL ) {
407                                 int             i;
408 #ifndef SLAP_NVALUES
409                                 struct berval   asserted;
410                                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
411                                         &mod->sm_bvalues[ 0 ], &asserted, text );
412                                 if ( rc != LDAP_SUCCESS ) {
413                                         return rc;
414                                 }
415 #endif
416
417                                 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
418                                         int     match;
419
420 #ifdef SLAP_NVALUES
421                                         if( mod->sm_nvalues ) {
422                                                 rc = value_match( &match, mod->sm_desc, mr,
423                                                         SLAP_MR_EQUALITY
424                                                                 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
425                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
426                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
427                                                         &a->a_nvals[i],
428                                                         &mod->sm_nvalues[0],
429                                                         text );
430
431                                         } else {
432                                                 rc = value_match( &match, mod->sm_desc, mr,
433                                                         SLAP_MR_EQUALITY
434                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
435                                                         &a->a_vals[i],
436                                                         &mod->sm_values[0],
437                                                         text );
438                                         }
439
440 #else
441                                         rc = value_match( &match, mod->sm_desc, mr,
442                                                 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
443                                                 &a->a_vals[i],
444                                                 &asserted,
445                                                 text );
446 #endif
447
448                                         if( rc == LDAP_SUCCESS && match == 0 ) {
449                                                 if ( permissive ) {
450                                                         matched++;
451                                                         continue;
452                                                 }
453 #ifndef SLAP_NVALUES
454                                                 free( asserted.bv_val );
455 #endif
456                                                 *text = textbuf;
457                                                 snprintf( textbuf, textlen,
458                                                         "modify/%s: %s: value #0 already exists",
459                                                         op, mod->sm_desc->ad_cname.bv_val, 0 );
460                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
461                                         }
462                                 }
463 #ifndef SLAP_NVALUES
464                                 free( asserted.bv_val );
465 #endif
466                                 if ( permissive && matched == i ) {
467                                         /* values already exist; do nothing */
468                                         return LDAP_SUCCESS;
469                                 }
470                         }
471
472                 } else {
473                         rc = modify_check_duplicates( mod->sm_desc, mr,
474                                         a ? a->a_vals : NULL, mod->sm_bvalues,
475                                         permissive, text, textbuf, textlen );
476
477                         if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
478                                 return LDAP_SUCCESS;
479                         }
480
481                         if ( rc != LDAP_SUCCESS ) {
482                                 return rc;
483                         }
484                 }
485         }
486
487         /* no - add them */
488 #ifdef SLAP_NVALUES
489         if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 )
490 #else
491         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )
492 #endif
493         {
494                 /* this should return result of attr_merge */
495                 *text = textbuf;
496                 snprintf( textbuf, textlen,
497                         "modify/%s: %s: merge error",
498                         op, mod->sm_desc->ad_cname.bv_val );
499                 return LDAP_OTHER;
500         }
501
502         return LDAP_SUCCESS;
503 }
504
505 int
506 modify_delete_values(
507         Entry   *e,
508         Modification    *mod,
509         int     permissive,
510         const char      **text,
511         char *textbuf, size_t textlen
512 )
513 {
514         int             i, j, k, rc = LDAP_SUCCESS;
515         Attribute       *a;
516         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
517 #ifndef SLAP_NVALUES
518         BerVarray       nvals = NULL;
519 #endif
520         char            dummy = '\0';
521
522         /*
523          * If permissive is set, then the non-existence of an 
524          * attribute is not treated as an error.
525          */
526
527         /* delete the entire attribute */
528         if ( mod->sm_bvalues == NULL ) {
529                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
530
531                 if( permissive ) {
532                         rc = LDAP_SUCCESS;
533                 } else if( rc != LDAP_SUCCESS ) {
534                         *text = textbuf;
535                         snprintf( textbuf, textlen,
536                                 "modify/delete: %s: no such attribute",
537                                 mod->sm_desc->ad_cname.bv_val );
538                         rc = LDAP_NO_SUCH_ATTRIBUTE;
539                 }
540                 return rc;
541         }
542
543         if( mr == NULL || !mr->smr_match ) {
544                 /* disallow specific attributes from being deleted if
545                         no equality rule */
546                 *text = textbuf;
547                 snprintf( textbuf, textlen,
548                         "modify/delete: %s: no equality matching rule",
549                         mod->sm_desc->ad_cname.bv_val );
550                 return LDAP_INAPPROPRIATE_MATCHING;
551         }
552
553         /* delete specific values - find the attribute first */
554         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
555                 if( permissive ) {
556                         return LDAP_SUCCESS;
557                 }
558                 *text = textbuf;
559                 snprintf( textbuf, textlen,
560                         "modify/delete: %s: no such attribute",
561                         mod->sm_desc->ad_cname.bv_val );
562                 return LDAP_NO_SUCH_ATTRIBUTE;
563         }
564
565 #ifndef SLAP_NVALUES
566         /* find each value to delete */
567         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ )
568                 /* count existing values */ ;
569
570         nvals = (BerVarray)SLAP_CALLOC( j + 1, sizeof ( struct berval ) );
571         if( nvals == NULL ) {
572 #ifdef NEW_LOGGING
573                 LDAP_LOG( OPERATION, ERR,
574                         "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
575 #else
576                 Debug( LDAP_DEBUG_ANY, 
577                         "modify_delete_values: SLAP_CALLOC failed", 0, 0, 0 );
578 #endif
579                                 goto return_results;
580         }
581
582         /* normalize existing values */
583         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ ) {
584                 rc = value_normalize( a->a_desc, SLAP_MR_EQUALITY,
585                         &a->a_vals[ j ], &nvals[ j ], text );
586
587                 if ( rc != LDAP_SUCCESS ) {
588                         nvals[ j ].bv_val = NULL;
589                         goto return_results;
590                 }
591         }
592 #endif
593
594         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
595                 int     found = 0;
596 #ifndef SLAP_NVALUES
597                 struct  berval asserted;
598
599                 /* normalize the value to be deleted */
600                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
601                         &mod->sm_bvalues[ i ], &asserted, text );
602
603                 if( rc != LDAP_SUCCESS ) {
604                         goto return_results;
605                 }
606
607                 /* search it */
608                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ )
609 #else
610                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ )
611 #endif
612                 {
613                         int match;
614
615 #ifndef SLAP_NVALUES
616                         if ( nvals[j].bv_val == &dummy ) {
617                                 continue;
618                         }
619
620 #endif
621 #ifdef SLAP_NVALUES
622                         if( mod->sm_nvalues ) {
623                                 assert( a->a_nvals );
624                                 rc = (*mr->smr_match)( &match,
625                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
626                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
627                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
628                                         a->a_desc->ad_type->sat_syntax,
629                                         mr, &a->a_nvals[j],
630                                         &mod->sm_nvalues[i] );
631                         } else {
632 #if 0
633                                 assert( a->a_nvals == NULL );
634 #endif
635                                 rc = (*mr->smr_match)( &match,
636                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
637                                         a->a_desc->ad_type->sat_syntax,
638                                         mr, &a->a_vals[j],
639                                         &mod->sm_values[i] );
640                         }
641 #else
642                         rc = (*mr->smr_match)( &match,
643                                 SLAP_MR_ATTRIBUTE_SYNTAX_MATCH,
644                                 a->a_desc->ad_type->sat_syntax,
645                                 mr, &nvals[ j ],
646                                 &asserted );
647 #endif
648
649                         if ( rc != LDAP_SUCCESS ) {
650 #ifndef SLAP_NVALUES
651                                 free( asserted.bv_val );
652 #endif
653                                 *text = textbuf;
654                                 snprintf( textbuf, textlen,
655                                         "%s: matching rule failed",
656                                         mod->sm_desc->ad_cname.bv_val );
657                                 goto return_results;
658                         }
659
660                         if ( match != 0 ) {
661                                 continue;
662                         }
663
664                         found = 1;
665
666                         /* delete value and mark it as dummy */
667 #ifdef SLAP_NVALUES
668                         free( a->a_vals[j].bv_val );
669                         a->a_vals[j].bv_val = &dummy;
670                         if( a->a_nvals != a->a_vals ) {
671                                 free( a->a_nvals[j].bv_val );
672                                 a->a_nvals[j].bv_val = &dummy;
673                         }
674 #else
675                         free( nvals[ j ].bv_val );
676                         nvals[ j ].bv_val = &dummy;
677 #endif
678
679                         break;
680                 }
681
682 #ifndef SLAP_NVALUES
683                 free( asserted.bv_val );
684 #endif
685
686                 if ( found == 0 ) {
687                         *text = textbuf;
688                         snprintf( textbuf, textlen,
689                                 "modify/delete: %s: no such value",
690                                 mod->sm_desc->ad_cname.bv_val );
691                         rc = LDAP_NO_SUCH_ATTRIBUTE;
692                         goto return_results;
693                 }
694         }
695
696         /* compact array skipping dummies */
697 #ifdef SLAP_NVALUES
698         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ )
699 #else
700         for ( k = 0, j = 0; nvals[k].bv_val != NULL; j++, k++ )
701 #endif
702         {
703 #ifdef SLAP_NVALUES
704                 /* skip dummies */
705                 if( a->a_vals[k].bv_val == &dummy ) {
706                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
707                         continue;
708                 }
709 #else
710                 /* delete and skip dummies */ ;
711                 for ( ; nvals[ k ].bv_val == &dummy; k++ ) {
712                         free( a->a_vals[ k ].bv_val );
713                 }
714 #endif
715                 if ( j != k ) {
716                         a->a_vals[ j ] = a->a_vals[ k ];
717 #ifdef SLAP_NVALUES
718                         if (a->a_nvals != a->a_vals) {
719                                 a->a_nvals[ j ] = a->a_nvals[ k ];
720                         }
721 #endif
722                 }
723
724 #ifndef SLAP_NVALUES
725                 if ( a->a_vals[ k ].bv_val == NULL ) {
726                         break;
727                 }
728 #else
729                 j++;
730 #endif
731         }
732
733         a->a_vals[j].bv_val = NULL;
734 #ifdef SLAP_NVALUES
735         if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
736 #else
737
738         assert( i == k - j );
739 #endif
740
741         /* if no values remain, delete the entire attribute */
742         if ( a->a_vals[0].bv_val == NULL ) {
743                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
744                         *text = textbuf;
745                         snprintf( textbuf, textlen,
746                                 "modify/delete: %s: no such attribute",
747                                 mod->sm_desc->ad_cname.bv_val );
748                         rc = LDAP_NO_SUCH_ATTRIBUTE;
749                 }
750         }
751
752 return_results:;
753 #ifndef SLAP_NVALUES
754         if ( nvals ) {
755                 /* delete the remaining normalized values */
756                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
757                         if ( nvals[ j ].bv_val != &dummy ) {
758                                 ber_memfree( nvals[ j ].bv_val );
759                         }
760                 }
761                 ber_memfree( nvals );
762         }
763 #endif
764
765         return rc;
766 }
767
768 int
769 modify_replace_values(
770         Entry   *e,
771         Modification    *mod,
772         int             permissive,
773         const char      **text,
774         char *textbuf, size_t textlen
775 )
776 {
777         (void) attr_delete( &e->e_attrs, mod->sm_desc );
778
779         if ( mod->sm_bvalues ) {
780                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
781         }
782
783         return LDAP_SUCCESS;
784 }
785
786 void
787 slap_mod_free(
788         Modification    *mod,
789         int                             freeit
790 )
791 {
792         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
793         mod->sm_values = NULL;
794
795 #ifdef SLAP_NVALUES
796         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
797         mod->sm_nvalues = NULL;
798 #endif
799
800         if( freeit ) free( mod );
801 }
802
803 void
804 slap_mods_free(
805     Modifications       *ml
806 )
807 {
808         Modifications *next;
809
810         for ( ; ml != NULL; ml = next ) {
811                 next = ml->sml_next;
812
813                 slap_mod_free( &ml->sml_mod, 0 );
814                 free( ml );
815         }
816 }
817