]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
Patch: Delete the buggy surrogate parent code (ITS#1815)
[openldap] / servers / slapd / mods.c
1 /*
2  * Copyright 1998-2002 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         const char      **text,
28         char *textbuf, size_t textlen )
29 {
30         int             i, j, numvals = 0, nummods,
31                         rc = LDAP_SUCCESS;
32         BerVarray       nvals = NULL, nmods;
33
34         /*
35          * FIXME: better do the following
36          * 
37          *   - count the existing values
38          *   - count the new values
39          *   
40          *   - if the existing values are less than the new ones {
41          *       - normalize all the existing values
42          *       - for each new value {
43          *           - normalize
44          *           - check with existing
45          *           - cross-check with already normalized new vals
46          *       }
47          *   } else {
48          *       - for each new value {
49          *           - normalize
50          *           - cross-check with already normalized new vals
51          *       }
52          *       - for each existing value {
53          *           - normalize
54          *           - check with already normalized new values
55          *       }
56          *   }
57          *
58          * The first case is good when adding a lot of new values,
59          * and significantly at first import of values (e.g. adding
60          * a new group); the latter case seems to be quite important
61          * as well, because it is likely to be the most frequently
62          * used when administering the entry.  The current 
63          * implementation will always normalize all the existing
64          * values before checking.  If there's no duplicate, the
65          * performances should not change; they will in case of error.
66          */
67
68         for ( nummods = 0; mods[ nummods ].bv_val != NULL; nummods++ )
69                 /* count new values */ ;
70
71         if ( vals ) {
72                 for ( numvals = 0; vals[ numvals ].bv_val != NULL; numvals++ )
73                         /* count existing values */ ;
74
75                 if ( numvals < nummods ) {
76                         nvals = ch_calloc( numvals + 1, sizeof( struct berval ) );
77
78                         /* normalize the existing values first */
79                         for ( j = 0; vals[ j ].bv_val != NULL; j++ ) {
80                                 rc = value_normalize( ad, SLAP_MR_EQUALITY,
81                                         &vals[ j ], &nvals[ j ], text );
82
83                                 /* existing attribute values must normalize */
84                                 assert( rc == LDAP_SUCCESS );
85
86                                 if ( rc != LDAP_SUCCESS ) {
87                                         nvals[ j ].bv_val = NULL;
88                                         goto return_results;
89                                 }
90                         }
91                         nvals[ j ].bv_val = NULL;
92                 }
93         }
94
95         /*
96          * If the existing values are less than the new values,
97          * it is more convenient to normalize all the existing
98          * values and test each new value against them first,
99          * then to other already normalized values
100          */
101         nmods = ch_calloc( nummods + 1, sizeof( struct berval ) );
102
103         for ( i = 0; mods[ i ].bv_val != NULL; i++ ) {
104                 rc = value_normalize( ad, SLAP_MR_EQUALITY,
105                         &mods[ i ], &nmods[ i ], text );
106
107                 if ( rc != LDAP_SUCCESS ) {
108                         nmods[ i ].bv_val = NULL;
109                         goto return_results;
110                 }
111
112                 if ( numvals > 0 && numvals < nummods ) {
113                         for ( j = 0; nvals[ j ].bv_val; j++ ) {
114                                 int match;
115
116                                 rc = (*mr->smr_match)( &match,
117                                         SLAP_MR_VALUE_SYNTAX_MATCH,
118                                         ad->ad_type->sat_syntax,
119                                         mr, &nmods[ i ], &nvals[ j ] );
120                                 if ( rc != LDAP_SUCCESS ) {
121                                         nmods[ i + 1 ].bv_val = NULL;
122                                         goto return_results;
123                                 }
124         
125                                 if ( match == 0 ) {
126                                         snprintf( textbuf, textlen,
127                                                 "%s: value #%d provided more than once",
128                                                 ad->ad_cname.bv_val, i );
129                                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
130                                         nmods[ i + 1 ].bv_val = NULL;
131                                         goto return_results;
132                                 }
133                         }
134                 }
135         
136                 for ( j = 0; j < i; j++ ) {
137                         int match;
138
139                         rc = (*mr->smr_match)( &match,
140                                 SLAP_MR_VALUE_SYNTAX_MATCH,
141                                 ad->ad_type->sat_syntax,
142                                 mr, &nmods[ i ], &nmods[ j ] );
143                         if ( rc != LDAP_SUCCESS ) {
144                                 nmods[ i + 1 ].bv_val = NULL;
145                                 goto return_results;
146                         }
147
148                         if ( match == 0 ) {
149                                 snprintf( textbuf, textlen,
150                                         "%s: value #%d provided more than once",
151                                         ad->ad_cname.bv_val, j );
152                                 rc = LDAP_TYPE_OR_VALUE_EXISTS;
153                                 nmods[ i + 1 ].bv_val = NULL;
154                                 goto return_results;
155                         }
156                 }
157         }
158         nmods[ i ].bv_val = NULL;
159
160         /*
161          * if new values are more than existing values, it is more
162          * convenient to normalize and check all new values first,
163          * then check each new value against existing values, which 
164          * can be normalized in place
165          */
166
167         if ( numvals >= nummods ) {
168                 for ( j = 0; vals[ j ].bv_val; j++ ) {
169                         struct berval   asserted;
170
171                         rc = value_normalize( ad, SLAP_MR_EQUALITY,
172                                 &vals[ j ], &asserted, text );
173
174                         if ( rc != LDAP_SUCCESS ) {
175                                 goto return_results;
176                         }
177
178                         for ( i = 0; nmods[ i ].bv_val; i++ ) {
179                                 int match;
180
181                                 rc = (*mr->smr_match)( &match,
182                                         SLAP_MR_VALUE_SYNTAX_MATCH,
183                                         ad->ad_type->sat_syntax,
184                                         mr, &nmods[ i ], &asserted );
185                                 if ( rc != LDAP_SUCCESS ) {
186                                         goto return_results;
187                                 }
188
189                                 if ( match == 0 ) {
190                                         snprintf( textbuf, textlen,
191                                                 "%s: value #%d provided more than once",
192                                                 ad->ad_cname.bv_val, j );
193                                         rc = LDAP_TYPE_OR_VALUE_EXISTS;
194                                         goto return_results;
195                                 }
196                         }
197
198                 }
199         }
200
201 return_results:;
202         if ( nvals ) {
203                 ber_bvarray_free( nvals );
204         }
205         if ( nmods ) {
206                 ber_bvarray_free( nmods );
207         }
208
209         return rc;
210 }
211
212 int
213 modify_add_values(
214         Entry   *e,
215         Modification    *mod,
216         const char      **text,
217         char *textbuf, size_t textlen
218 )
219 {
220         int             i, j;
221         Attribute       *a;
222         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
223         const char *op;
224
225         switch( mod->sm_op ) {
226         case LDAP_MOD_ADD:
227                 op = "add";
228                 break;
229         case LDAP_MOD_REPLACE:
230                 op = "replace";
231                 break;
232         default:
233                 op = "?";
234                 assert( 0 );
235         }
236
237         a = attr_find( e->e_attrs, mod->sm_desc );
238
239         /* check if the values we're adding already exist */
240         if( mr == NULL || !mr->smr_match ) {
241                 if ( a != NULL ) {
242                         /* do not allow add of additional attribute
243                                 if no equality rule exists */
244                         *text = textbuf;
245                         snprintf( textbuf, textlen,
246                                 "modify/%s: %s: no equality matching rule",
247                                 op, mod->sm_desc->ad_cname.bv_val );
248                         return LDAP_INAPPROPRIATE_MATCHING;
249                 }
250
251                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
252                         /* test asserted values against existing values */
253                         if( a ) {
254                                 for( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
255                                         if ( bvmatch( &mod->sm_bvalues[i],
256                                                 &a->a_vals[j] ) ) {
257
258                                                 /* value exists already */
259                                                 *text = textbuf;
260                                                 snprintf( textbuf, textlen,
261                                                         "modify/%s: %s: value #%i already exists",
262                                                         op, mod->sm_desc->ad_cname.bv_val, j );
263                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
264                                         }
265                                 }
266                         }
267
268                         /* test asserted values against themselves */
269                         for( j = 0; j < i; j++ ) {
270                                 if ( bvmatch( &mod->sm_bvalues[i],
271                                         &mod->sm_bvalues[j] ) ) {
272
273                                         /* value exists already */
274                                         *text = textbuf;
275                                         snprintf( textbuf, textlen,
276                                                 "modify/%s: %s: value #%i already exists",
277                                                 op, mod->sm_desc->ad_cname.bv_val, j );
278                                         return LDAP_TYPE_OR_VALUE_EXISTS;
279                                 }
280                         }
281                 }
282
283         } else {
284                 /*
285                  * The original code performs ( n ) normalizations 
286                  * and ( n * ( n - 1 ) / 2 ) matches, which hide
287                  * the same number of normalizations.  The new code
288                  * performs the same number of normalizations ( n )
289                  * and ( n * ( n - 1 ) / 2 ) mem compares, far less
290                  * expensive than an entire match, if a match is
291                  * equivalent to a normalization and a mem compare ...
292                  * 
293                  * This is far more memory expensive than the previous,
294                  * but it can heavily improve performances when big
295                  * chunks of data are added (typical example is a group
296                  * with thousands of DN-syntax members; on my system:
297                  * for members of 5-RDN DNs,
298
299                 members         orig            bvmatch (dirty) new
300                 1000            0m38.456s       0m0.553s        0m0.608s
301                 2000            2m33.341s       0m0.851s        0m1.003s
302
303                  * Moreover, 100 groups with 10000 members each were
304                  * added in 37m27.933s (an analogous LDIF file was
305                  * loaded into Active Directory in 38m28.682s, BTW).
306                  * 
307                  * Maybe we could switch to the new algorithm when
308                  * the number of values overcomes a given threshold?
309                  */
310
311                 int             rc;
312
313                 if ( mod->sm_bvalues[ 1 ].bv_val == 0 ) {
314                         if ( a != NULL ) {
315                                 struct berval   asserted;
316                                 int             i;
317
318                                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
319                                         &mod->sm_bvalues[ 0 ], &asserted, text );
320
321                                 if ( rc != LDAP_SUCCESS ) {
322                                         return rc;
323                                 }
324
325                                 for ( i = 0; a->a_vals[ i ].bv_val; i++ ) {
326                                         int     match;
327
328                                         rc = value_match( &match, mod->sm_desc, mr,
329                                                 SLAP_MR_VALUE_SYNTAX_MATCH,
330                                                 &a->a_vals[ i ], &asserted, text );
331
332                                         if( rc == LDAP_SUCCESS && match == 0 ) {
333                                                 free( asserted.bv_val );
334                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
335                                         }
336                                 }
337                         }
338
339                 } else {
340                         rc = modify_check_duplicates( mod->sm_desc, mr,
341                                         a ? a->a_vals : NULL, mod->sm_bvalues,
342                                         text, textbuf, textlen );
343         
344                         if ( rc != LDAP_SUCCESS ) {
345                                 return rc;
346                         }
347                 }
348         }
349
350         /* no - add them */
351         if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
352                 /* this should return result of attr_merge */
353                 *text = textbuf;
354                 snprintf( textbuf, textlen,
355                         "modify/%s: %s: merge error",
356                         op, mod->sm_desc->ad_cname.bv_val );
357                 return LDAP_OTHER;
358         }
359
360         return LDAP_SUCCESS;
361 }
362
363 int
364 modify_delete_values(
365         Entry   *e,
366         Modification    *mod,
367         const char      **text,
368         char *textbuf, size_t textlen
369 )
370 {
371         int             i, j, k, rc = LDAP_SUCCESS;
372         Attribute       *a;
373         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
374         BerVarray       nvals = NULL;
375         char            dummy = '\0';
376
377         /* delete the entire attribute */
378         if ( mod->sm_bvalues == NULL ) {
379                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
380
381                 if( rc != LDAP_SUCCESS ) {
382                         *text = textbuf;
383                         snprintf( textbuf, textlen,
384                                 "modify/delete: %s: no such attribute",
385                                 mod->sm_desc->ad_cname.bv_val );
386                         rc = LDAP_NO_SUCH_ATTRIBUTE;
387                 }
388                 return rc;
389         }
390
391         if( mr == NULL || !mr->smr_match ) {
392                 /* disallow specific attributes from being deleted if
393                         no equality rule */
394                 *text = textbuf;
395                 snprintf( textbuf, textlen,
396                         "modify/delete: %s: no equality matching rule",
397                         mod->sm_desc->ad_cname.bv_val );
398                 return LDAP_INAPPROPRIATE_MATCHING;
399         }
400
401         /* delete specific values - find the attribute first */
402         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
403                 *text = textbuf;
404                 snprintf( textbuf, textlen,
405                         "modify/delete: %s: no such attribute",
406                         mod->sm_desc->ad_cname.bv_val );
407                 return LDAP_NO_SUCH_ATTRIBUTE;
408         }
409
410         /* find each value to delete */
411         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ )
412                 /* count existing values */ ;
413
414         nvals = (BerVarray)ch_calloc( j + 1, sizeof ( struct berval ) );
415
416         /* normalize existing values */
417         for ( j = 0; a->a_vals[ j ].bv_val != NULL; j++ ) {
418                 rc = value_normalize( a->a_desc, SLAP_MR_EQUALITY,
419                         &a->a_vals[ j ], &nvals[ j ], text );
420
421                 if ( rc != LDAP_SUCCESS ) {
422                         nvals[ j ].bv_val = NULL;
423                         goto return_results;
424                 }
425         }
426
427         for ( i = 0; mod->sm_bvalues[ i ].bv_val != NULL; i++ ) {
428                 struct  berval asserted;
429                 int     found = 0;
430
431                 /* normalize the value to be deleted */
432                 rc = value_normalize( mod->sm_desc, SLAP_MR_EQUALITY,
433                         &mod->sm_bvalues[ i ], &asserted, text );
434
435                 if( rc != LDAP_SUCCESS ) {
436                         goto return_results;
437                 }
438
439                 /* search it */
440                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
441                         int match;
442
443                         if ( nvals[ j ].bv_val == &dummy ) {
444                                 continue;
445                         }
446
447                         rc = (*mr->smr_match)( &match,
448                                 SLAP_MR_VALUE_SYNTAX_MATCH,
449                                 a->a_desc->ad_type->sat_syntax,
450                                 mr, &nvals[ j ], &asserted );
451
452                         if ( rc != LDAP_SUCCESS ) {
453                                 free( asserted.bv_val );
454                                 goto return_results;
455                         }
456
457                         if ( match != 0 ) {
458                                 continue;
459                         }
460
461                         found = 1;
462
463                         /* delete value and mark it as dummy */
464                         free( nvals[ j ].bv_val );
465                         nvals[ j ].bv_val = &dummy;
466
467                         break;
468                 }
469
470                 free( asserted.bv_val );
471
472                 if ( found == 0 ) {
473                         *text = textbuf;
474                         snprintf( textbuf, textlen,
475                                 "modify/delete: %s: no such value",
476                                 mod->sm_desc->ad_cname.bv_val );
477                         rc = LDAP_NO_SUCH_ATTRIBUTE;
478                         goto return_results;
479                 }
480         }
481
482         /* compact array skipping dummies */
483         for ( k = 0, j = 0; nvals[ k ].bv_val != NULL; j++, k++ ) {
484
485                 /* delete and skip dummies */ ;
486                 for ( ; nvals[ k ].bv_val == &dummy; k++ ) {
487                         free( a->a_vals[ k ].bv_val );
488                 }
489
490                 if ( j != k ) {
491                         a->a_vals[ j ] = a->a_vals[ k ];
492                 }
493
494                 if ( a->a_vals[ k ].bv_val == NULL ) {
495                         break;
496                 }
497         }
498         a->a_vals[ j ].bv_val = NULL;
499
500         assert( i == k - j );
501
502         /* if no values remain, delete the entire attribute */
503         if ( a->a_vals[0].bv_val == NULL ) {
504                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
505                         *text = textbuf;
506                         snprintf( textbuf, textlen,
507                                 "modify/delete: %s: no such attribute",
508                                 mod->sm_desc->ad_cname.bv_val );
509                         rc = LDAP_NO_SUCH_ATTRIBUTE;
510                 }
511         }
512
513 return_results:;
514         if ( nvals ) {
515                 /* delete the remaining normalized values */
516                 for ( j = 0; nvals[ j ].bv_val != NULL; j++ ) {
517                         if ( nvals[ j ].bv_val != &dummy ) {
518                                 ber_memfree( nvals[ j ].bv_val );
519                         }
520                 }
521                 ber_memfree( nvals );
522         }
523
524         return rc;
525 }
526
527 int
528 modify_replace_values(
529         Entry   *e,
530         Modification    *mod,
531         const char      **text,
532         char *textbuf, size_t textlen
533 )
534 {
535         (void) attr_delete( &e->e_attrs, mod->sm_desc );
536
537         if ( mod->sm_bvalues ) {
538                 return modify_add_values( e, mod, text, textbuf, textlen );
539         }
540
541         return LDAP_SUCCESS;
542 }
543
544 void
545 slap_mod_free(
546         Modification    *mod,
547         int                             freeit
548 )
549 {
550 #if 0
551         if ( mod->sm_type.bv_val)
552                 free( mod->sm_type.bv_val );
553 #endif
554         if ( mod->sm_bvalues != NULL )
555                 ber_bvarray_free( mod->sm_bvalues );
556
557         if( freeit )
558                 free( mod );
559 }
560
561 void
562 slap_mods_free(
563     Modifications       *ml
564 )
565 {
566         Modifications *next;
567
568         for ( ; ml != NULL; ml = next ) {
569                 next = ml->sml_next;
570
571                 slap_mod_free( &ml->sml_mod, 0 );
572                 free( ml );
573         }
574 }
575