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