]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
Fix for Howard's fix for ITS #2704
[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 <ac/string.h>
20
21 #include "slap.h"
22
23 int
24 modify_check_duplicates(
25         AttributeDescription    *ad,
26         MatchingRule            *mr,
27         BerVarray               vals,
28         BerVarray               mods,
29         int                     permissive,
30         const char      **text,
31         char *textbuf, size_t textlen )
32 {
33         int             i, j, numvals = 0, nummods,
34                         rc = LDAP_SUCCESS, matched;
35         /* this function is no longer used */
36         return rc;
37 }
38
39 int
40 modify_add_values(
41         Entry   *e,
42         Modification    *mod,
43         int     permissive,
44         const char      **text,
45         char *textbuf, size_t textlen )
46 {
47         int             i, j;
48         int             matched;
49         Attribute       *a;
50         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
51         const char *op;
52
53         switch( mod->sm_op ) {
54         case LDAP_MOD_ADD:
55                 op = "add";
56                 break;
57         case LDAP_MOD_REPLACE:
58                 op = "replace";
59                 break;
60         default:
61                 op = "?";
62                 assert( 0 );
63         }
64
65         a = attr_find( e->e_attrs, mod->sm_desc );
66
67         /*
68          * With permissive set, as long as the attribute being added
69          * has the same value(s?) as the existing attribute, then the
70          * modify will succeed.
71          */
72
73         /* check if the values we're adding already exist */
74         if( mr == NULL || !mr->smr_match ) {
75                 if ( a != NULL ) {
76                         /* do not allow add of additional attribute
77                                 if no equality rule exists */
78                         *text = textbuf;
79                         snprintf( textbuf, textlen,
80                                 "modify/%s: %s: no equality matching rule",
81                                 op, mod->sm_desc->ad_cname.bv_val );
82                         return LDAP_INAPPROPRIATE_MATCHING;
83                 }
84
85                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
86                         /* test asserted values against existing values */
87                         if( a ) {
88                                 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
89                                         if ( bvmatch( &mod->sm_bvalues[i], &a->a_vals[j] ) ) {
90                                                 if ( permissive ) {
91                                                         matched++;
92                                                         continue;
93                                                 }
94                                                 /* value exists already */
95                                                 *text = textbuf;
96                                                 snprintf( textbuf, textlen,
97                                                         "modify/%s: %s: value #%i already exists",
98                                                         op, mod->sm_desc->ad_cname.bv_val, j );
99                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
100                                         }
101                                 }
102                                 if ( permissive && matched == j ) {
103                                         /* values already exist; do nothing */
104                                         return LDAP_SUCCESS;
105                                 }
106                         }
107
108                         /* test asserted values against themselves */
109                         for( j = 0; j < i; j++ ) {
110                                 if ( bvmatch( &mod->sm_bvalues[i], &mod->sm_bvalues[j] ) ) {
111                                         /* value exists already */
112                                         *text = textbuf;
113                                         snprintf( textbuf, textlen,
114                                                 "modify/%s: %s: value #%i already exists",
115                                                 op, mod->sm_desc->ad_cname.bv_val, j );
116                                         return LDAP_TYPE_OR_VALUE_EXISTS;
117                                 }
118                         }
119                 }
120
121         } else {
122                 /* no normalization is done in this routine nor
123                  * in the matching routines called by this routine. 
124                  * values are now normalized once on input to the
125                  * server (whether from LDAP or from the underlying
126                  * database).
127                  * This should outperform the old code.  No numbers
128                  * are available yet.
129                  */
130
131                 int             rc;
132
133                 if ( mod->sm_bvalues[1].bv_val == 0 ) {
134                         if ( a != NULL ) {
135                                 int             i;
136
137                                 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
138                                         int     match;
139
140                                         if( mod->sm_nvalues ) {
141                                                 rc = value_match( &match, mod->sm_desc, mr,
142                                                         SLAP_MR_EQUALITY
143                                                                 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
144                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
145                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
146                                                         &a->a_nvals[i],
147                                                         &mod->sm_nvalues[0],
148                                                         text );
149
150                                         } else {
151                                                 rc = value_match( &match, mod->sm_desc, mr,
152                                                         SLAP_MR_EQUALITY
153                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
154                                                         &a->a_vals[i],
155                                                         &mod->sm_values[0],
156                                                         text );
157                                         }
158
159
160                                         if( rc == LDAP_SUCCESS && match == 0 ) {
161                                                 if ( permissive ) {
162                                                         matched++;
163                                                         continue;
164                                                 }
165                                                 *text = textbuf;
166                                                 snprintf( textbuf, textlen,
167                                                         "modify/%s: %s: value #0 already exists",
168                                                         op, mod->sm_desc->ad_cname.bv_val );
169                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
170                                         }
171                                 }
172                                 if ( permissive && matched == i ) {
173                                         /* values already exist; do nothing */
174                                         return LDAP_SUCCESS;
175                                 }
176                         }
177
178                 } else {
179                         rc = modify_check_duplicates( mod->sm_desc, mr,
180                                 a ? a->a_vals : NULL, mod->sm_bvalues,
181                                 permissive, text, textbuf, textlen );
182
183                         if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
184                                 return LDAP_SUCCESS;
185                         }
186
187                         if ( rc != LDAP_SUCCESS ) {
188                                 return rc;
189                         }
190                 }
191         }
192
193         /* no - add them */
194         if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
195                 /* this should return result of attr_merge */
196                 *text = textbuf;
197                 snprintf( textbuf, textlen,
198                         "modify/%s: %s: merge error",
199                         op, mod->sm_desc->ad_cname.bv_val );
200                 return LDAP_OTHER;
201         }
202
203         return LDAP_SUCCESS;
204 }
205
206 int
207 modify_delete_values(
208         Entry   *e,
209         Modification    *mod,
210         int     permissive,
211         const char      **text,
212         char *textbuf, size_t textlen
213 )
214 {
215         int             i, j, k, rc = LDAP_SUCCESS;
216         Attribute       *a;
217         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
218         char            dummy = '\0';
219
220         /*
221          * If permissive is set, then the non-existence of an 
222          * attribute is not treated as an error.
223          */
224
225         /* delete the entire attribute */
226         if ( mod->sm_bvalues == NULL ) {
227                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
228
229                 if( permissive ) {
230                         rc = LDAP_SUCCESS;
231                 } else if( rc != LDAP_SUCCESS ) {
232                         *text = textbuf;
233                         snprintf( textbuf, textlen,
234                                 "modify/delete: %s: no such attribute",
235                                 mod->sm_desc->ad_cname.bv_val );
236                         rc = LDAP_NO_SUCH_ATTRIBUTE;
237                 }
238                 return rc;
239         }
240
241         if( mr == NULL || !mr->smr_match ) {
242                 /* disallow specific attributes from being deleted if
243                         no equality rule */
244                 *text = textbuf;
245                 snprintf( textbuf, textlen,
246                         "modify/delete: %s: no equality matching rule",
247                         mod->sm_desc->ad_cname.bv_val );
248                 return LDAP_INAPPROPRIATE_MATCHING;
249         }
250
251         /* delete specific values - find the attribute first */
252         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
253                 if( permissive ) {
254                         return LDAP_SUCCESS;
255                 }
256                 *text = textbuf;
257                 snprintf( textbuf, textlen,
258                         "modify/delete: %s: no such attribute",
259                         mod->sm_desc->ad_cname.bv_val );
260                 return LDAP_NO_SUCH_ATTRIBUTE;
261         }
262
263
264         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
265                 int     found = 0;
266                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
267                         int match;
268
269                         if( mod->sm_nvalues ) {
270                                 assert( a->a_nvals );
271                                 rc = (*mr->smr_match)( &match,
272                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
273                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
274                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
275                                         a->a_desc->ad_type->sat_syntax,
276                                         mr, &a->a_nvals[j],
277                                         &mod->sm_nvalues[i] );
278                         } else {
279 #if 0
280                                 assert( a->a_nvals == NULL );
281 #endif
282                                 rc = (*mr->smr_match)( &match,
283                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
284                                         a->a_desc->ad_type->sat_syntax,
285                                         mr, &a->a_vals[j],
286                                         &mod->sm_values[i] );
287                         }
288
289                         if ( rc != LDAP_SUCCESS ) {
290                                 *text = textbuf;
291                                 snprintf( textbuf, textlen,
292                                         "%s: matching rule failed",
293                                         mod->sm_desc->ad_cname.bv_val );
294                                 goto return_results;
295                         }
296
297                         if ( match != 0 ) {
298                                 continue;
299                         }
300
301                         found = 1;
302
303                         /* delete value and mark it as dummy */
304                         free( a->a_vals[j].bv_val );
305                         a->a_vals[j].bv_val = &dummy;
306                         if( a->a_nvals != a->a_vals ) {
307                                 free( a->a_nvals[j].bv_val );
308                                 a->a_nvals[j].bv_val = &dummy;
309                         }
310
311                         break;
312                 }
313
314                 if ( found == 0 ) {
315                         *text = textbuf;
316                         snprintf( textbuf, textlen,
317                                 "modify/delete: %s: no such value",
318                                 mod->sm_desc->ad_cname.bv_val );
319                         rc = LDAP_NO_SUCH_ATTRIBUTE;
320                         goto return_results;
321                 }
322         }
323
324         /* compact array skipping dummies */
325         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
326                 /* skip dummies */
327                 if( a->a_vals[k].bv_val == &dummy ) {
328                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
329                         continue;
330                 }
331                 if ( j != k ) {
332                         a->a_vals[ j ] = a->a_vals[ k ];
333                         if (a->a_nvals != a->a_vals) {
334                                 a->a_nvals[ j ] = a->a_nvals[ k ];
335                         }
336                 }
337
338                 j++;
339         }
340
341         a->a_vals[j].bv_val = NULL;
342         if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
343
344         /* if no values remain, delete the entire attribute */
345         if ( a->a_vals[0].bv_val == NULL ) {
346                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
347                         *text = textbuf;
348                         snprintf( textbuf, textlen,
349                                 "modify/delete: %s: no such attribute",
350                                 mod->sm_desc->ad_cname.bv_val );
351                         rc = LDAP_NO_SUCH_ATTRIBUTE;
352                 }
353         }
354
355 return_results:;
356
357         return rc;
358 }
359
360 int
361 modify_replace_values(
362         Entry   *e,
363         Modification    *mod,
364         int             permissive,
365         const char      **text,
366         char *textbuf, size_t textlen )
367 {
368         (void) attr_delete( &e->e_attrs, mod->sm_desc );
369
370         if ( mod->sm_bvalues ) {
371                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
372         }
373
374         return LDAP_SUCCESS;
375 }
376
377 int
378 modify_increment_values(
379         Entry   *e,
380         Modification    *mod,
381         int     permissive,
382         const char      **text,
383         char *textbuf, size_t textlen )
384 {
385         Attribute *a;
386
387         a = attr_find( e->e_attrs, mod->sm_desc );
388         if( a == NULL ) {
389                 *text = textbuf;
390                 snprintf( textbuf, textlen,
391                         "modify/increment: %s: no such attribute",
392                         mod->sm_desc->ad_cname.bv_val );
393                 return LDAP_NO_SUCH_ATTRIBUTE;
394         }
395
396
397         if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
398                 int i;
399                 char str[sizeof(long)*3 + 2]; /* overly long */
400                 long incr = atol( mod->sm_bvalues[0].bv_val );
401
402                 /* treat zero and errors as a no-op */
403                 if( incr == 0 ) {
404                         return LDAP_SUCCESS;
405                 }
406
407                 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
408                         char *tmp;
409                         long value = atol( a->a_nvals[i].bv_val );
410                         size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
411
412                         tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
413                         if( tmp == NULL ) {
414                                 *text = "modify/increment: reallocation error";
415                                 return LDAP_OTHER;;
416                         }
417                         a->a_nvals[i].bv_val = tmp;
418                         a->a_nvals[i].bv_len = strln;
419
420                         AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
421                 }
422
423         } else {
424                 snprintf( textbuf, textlen,
425                         "modify/increment: %s: increment not supported for value syntax %s",
426                         mod->sm_desc->ad_cname.bv_val,
427                         a->a_desc->ad_type->sat_syntax_oid );
428                 return LDAP_CONSTRAINT_VIOLATION;
429         }
430
431         return LDAP_SUCCESS;
432 }
433
434 void
435 slap_mod_free(
436         Modification    *mod,
437         int                             freeit )
438 {
439         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
440         mod->sm_values = NULL;
441
442         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
443         mod->sm_nvalues = NULL;
444
445         if( freeit ) free( mod );
446 }
447
448 void
449 slap_mods_free(
450     Modifications       *ml )
451 {
452         Modifications *next;
453
454         for ( ; ml != NULL; ml = next ) {
455                 next = ml->sml_next;
456
457                 slap_mod_free( &ml->sml_mod, 0 );
458                 free( ml );
459         }
460 }
461