]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
956ccfb78f3ddb1a1333443c8d8b21f8fc78633c
[openldap] / servers / slapd / mods.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <ac/string.h>
29
30 #include "slap.h"
31
32 int
33 modify_add_values(
34         Entry           *e,
35         Modification    *mod,
36         int             permissive,
37         const char      **text,
38         char            *textbuf,
39         size_t          textlen )
40 {
41         int             rc;
42         const char      *op;
43         Attribute       *a;
44         Modification    pmod = *mod;
45
46         switch ( mod->sm_op ) {
47         case LDAP_MOD_ADD:
48                 op = "add";
49                 break;
50         case LDAP_MOD_REPLACE:
51                 op = "replace";
52                 break;
53         default:
54                 op = "?";
55                 assert( 0 );
56         }
57
58         /* check if values to add exist in attribute */
59         a = attr_find( e->e_attrs, mod->sm_desc );
60         if ( a != NULL ) {
61                 int             rc, i, j, p;
62                 MatchingRule    *mr;
63
64                 mr = mod->sm_desc->ad_type->sat_equality;
65                 if( mr == NULL || !mr->smr_match ) {
66                         /* do not allow add of additional attribute
67                                 if no equality rule exists */
68                         *text = textbuf;
69                         snprintf( textbuf, textlen,
70                                 "modify/%s: %s: no equality matching rule",
71                                 op, mod->sm_desc->ad_cname.bv_val );
72                         return LDAP_INAPPROPRIATE_MATCHING;
73                 }
74
75                 if ( permissive ) {
76                         for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) /* count 'em */;
77                         pmod.sm_values = (BerVarray)ch_malloc( (i + 1)*sizeof( struct berval ) );
78                         if ( pmod.sm_nvalues != NULL ) {
79                                 pmod.sm_nvalues = (BerVarray)ch_malloc(
80                                         (i + 1)*sizeof( struct berval ) );
81                         }
82                 }
83
84                 /* no normalization is done in this routine nor
85                  * in the matching routines called by this routine. 
86                  * values are now normalized once on input to the
87                  * server (whether from LDAP or from the underlying
88                  * database).
89                  */
90                 for ( p = i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
91                         int     match;
92
93                         assert( a->a_vals[0].bv_val );
94                         for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
95                                 if ( mod->sm_nvalues ) {
96                                         rc = ordered_value_match( &match, mod->sm_desc, mr,
97                                                 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
98                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
99                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
100                                                 &a->a_nvals[j], &mod->sm_nvalues[i], text );
101                                 } else {
102                                         rc = ordered_value_match( &match, mod->sm_desc, mr,
103                                                 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
104                                                 &a->a_vals[j], &mod->sm_values[i], text );
105                                 }
106
107                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
108                                         /* value already exists */
109                                         if ( permissive ) break;
110
111                                         *text = textbuf;
112                                         snprintf( textbuf, textlen,
113                                                 "modify/%s: %s: value #%d already exists",
114                                                 op, mod->sm_desc->ad_cname.bv_val, i );
115                                         return LDAP_TYPE_OR_VALUE_EXISTS;
116
117                                 } else if ( rc != LDAP_SUCCESS ) {
118                                         return rc;
119                                 }
120                         }
121
122                         if ( permissive && match != 0 ) {
123                                 if ( pmod.sm_nvalues ) {
124                                         pmod.sm_nvalues[p] = mod->sm_nvalues[i];
125                                 }
126                                 pmod.sm_values[p++] = mod->sm_values[i];
127                         }
128                 }
129
130                 if ( permissive ) {
131                         if ( p == 0 ) {
132                                 /* all new values match exist */
133                                 ch_free( pmod.sm_values );
134                                 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
135                                 return LDAP_SUCCESS;
136                         }
137
138                         BER_BVZERO( &pmod.sm_values[p] );
139                         if ( pmod.sm_nvalues ) {
140                                 BER_BVZERO( &pmod.sm_nvalues[p] );
141                         }
142                 }
143         }
144
145         /* no - add them */
146         if ( mod->sm_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
147                 rc = ordered_value_add( e, mod->sm_desc, a,
148                         pmod.sm_values, pmod.sm_nvalues );
149         } else {
150                 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
151         }
152
153         if ( a != NULL && permissive ) {
154                 ch_free( pmod.sm_values );
155                 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
156         }
157
158         if ( rc != 0 ) {
159                 /* this should return result of attr_merge */
160                 *text = textbuf;
161                 snprintf( textbuf, textlen,
162                         "modify/%s: %s: merge error",
163                         op, mod->sm_desc->ad_cname.bv_val );
164                 return LDAP_OTHER;
165         }
166
167         return LDAP_SUCCESS;
168 }
169
170 int
171 modify_delete_values(
172         Entry   *e,
173         Modification    *mod,
174         int     permissive,
175         const char      **text,
176         char *textbuf, size_t textlen )
177 {
178         int             i, j, k, rc = LDAP_SUCCESS;
179         Attribute       *a;
180         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
181         char            dummy = '\0';
182         int             match = 0;
183
184         /*
185          * If permissive is set, then the non-existence of an 
186          * attribute is not treated as an error.
187          */
188
189         /* delete the entire attribute */
190         if ( mod->sm_values == NULL ) {
191                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
192
193                 if( permissive ) {
194                         rc = LDAP_SUCCESS;
195                 } else if( rc != LDAP_SUCCESS ) {
196                         *text = textbuf;
197                         snprintf( textbuf, textlen,
198                                 "modify/delete: %s: no such attribute",
199                                 mod->sm_desc->ad_cname.bv_val );
200                         rc = LDAP_NO_SUCH_ATTRIBUTE;
201                 }
202                 return rc;
203         }
204
205         if( mr == NULL || !mr->smr_match ) {
206                 /* disallow specific attributes from being deleted if
207                         no equality rule */
208                 *text = textbuf;
209                 snprintf( textbuf, textlen,
210                         "modify/delete: %s: no equality matching rule",
211                         mod->sm_desc->ad_cname.bv_val );
212                 return LDAP_INAPPROPRIATE_MATCHING;
213         }
214
215         /* delete specific values - find the attribute first */
216         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
217                 if( permissive ) {
218                         return LDAP_SUCCESS;
219                 }
220                 *text = textbuf;
221                 snprintf( textbuf, textlen,
222                         "modify/delete: %s: no such attribute",
223                         mod->sm_desc->ad_cname.bv_val );
224                 return LDAP_NO_SUCH_ATTRIBUTE;
225         }
226
227         for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
228                 int     found = 0;
229                 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
230                         /* skip already deleted values */
231                         if ( a->a_vals[j].bv_val == &dummy ) {
232                                 continue;
233                         }
234
235                         if( mod->sm_nvalues ) {
236                                 assert( a->a_nvals );
237                                 rc = ordered_value_match( &match, a->a_desc, mr,
238                                         SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
239                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
240                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
241                                         &a->a_nvals[j], &mod->sm_nvalues[i], text );
242                         } else {
243 #if 0
244                                 assert( a->a_nvals == NULL );
245 #endif
246                                 rc = ordered_value_match( &match, a->a_desc, mr,
247                                         SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
248                                         &a->a_vals[j], &mod->sm_values[i], text );
249                         }
250
251                         if ( rc != LDAP_SUCCESS ) {
252                                 *text = textbuf;
253                                 snprintf( textbuf, textlen,
254                                         "%s: matching rule failed",
255                                         mod->sm_desc->ad_cname.bv_val );
256                                 break;
257                         }
258
259                         if ( match != 0 ) {
260                                 continue;
261                         }
262
263                         found = 1;
264
265                         /* delete value and mark it as dummy */
266                         free( a->a_vals[j].bv_val );
267                         a->a_vals[j].bv_val = &dummy;
268                         if( a->a_nvals != a->a_vals ) {
269                                 free( a->a_nvals[j].bv_val );
270                                 a->a_nvals[j].bv_val = &dummy;
271                         }
272
273                         break;
274                 }
275
276                 if ( found == 0 ) {
277                         *text = textbuf;
278                         snprintf( textbuf, textlen,
279                                 "modify/delete: %s: no such value",
280                                 mod->sm_desc->ad_cname.bv_val );
281                         rc = LDAP_NO_SUCH_ATTRIBUTE;
282                         if ( i > 0 ) {
283                                 break;
284                         } else {
285                                 goto return_results;
286                         }
287                 }
288         }
289
290         /* compact array skipping dummies */
291         for ( k = 0, j = 0; !BER_BVISNULL( &a->a_vals[k] ); k++ ) {
292                 /* skip dummies */
293                 if( a->a_vals[k].bv_val == &dummy ) {
294                         assert( a->a_nvals[k].bv_val == &dummy );
295                         continue;
296                 }
297                 if ( j != k ) {
298                         a->a_vals[ j ] = a->a_vals[ k ];
299                         if (a->a_nvals != a->a_vals) {
300                                 a->a_nvals[ j ] = a->a_nvals[ k ];
301                         }
302                 }
303
304                 j++;
305         }
306
307         BER_BVZERO( &a->a_vals[j] );
308         if (a->a_nvals != a->a_vals) {
309                 BER_BVZERO( &a->a_nvals[j] );
310         }
311
312         /* if no values remain, delete the entire attribute */
313         if ( BER_BVISNULL( &a->a_vals[0] ) ) {
314                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
315                         *text = textbuf;
316                         snprintf( textbuf, textlen,
317                                 "modify/delete: %s: no such attribute",
318                                 mod->sm_desc->ad_cname.bv_val );
319                         rc = LDAP_NO_SUCH_ATTRIBUTE;
320                 }
321         } else if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED ) {
322         /* For an ordered attribute, renumber the value indices */
323                 ordered_value_sort( a, 1 );
324         }
325
326 return_results:;
327
328         return rc;
329 }
330
331 int
332 modify_replace_values(
333         Entry   *e,
334         Modification    *mod,
335         int             permissive,
336         const char      **text,
337         char *textbuf, size_t textlen )
338 {
339         (void) attr_delete( &e->e_attrs, mod->sm_desc );
340
341         if ( mod->sm_values ) {
342                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
343         }
344
345         return LDAP_SUCCESS;
346 }
347
348 int
349 modify_increment_values(
350         Entry   *e,
351         Modification    *mod,
352         int     permissive,
353         const char      **text,
354         char *textbuf, size_t textlen )
355 {
356         Attribute *a;
357
358         a = attr_find( e->e_attrs, mod->sm_desc );
359         if( a == NULL ) {
360                 *text = textbuf;
361                 snprintf( textbuf, textlen,
362                         "modify/increment: %s: no such attribute",
363                         mod->sm_desc->ad_cname.bv_val );
364                 return LDAP_NO_SUCH_ATTRIBUTE;
365         }
366
367         if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
368                 int i;
369                 char str[sizeof(long)*3 + 2]; /* overly long */
370                 long incr = atol( mod->sm_values[0].bv_val );
371
372                 /* treat zero and errors as a no-op */
373                 if( incr == 0 ) {
374                         return LDAP_SUCCESS;
375                 }
376
377                 for( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
378                         char *tmp;
379                         long value = atol( a->a_nvals[i].bv_val );
380                         size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
381
382                         tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
383                         if( tmp == NULL ) {
384                                 *text = "modify/increment: reallocation error";
385                                 return LDAP_OTHER;;
386                         }
387                         a->a_nvals[i].bv_val = tmp;
388                         a->a_nvals[i].bv_len = strln;
389
390                         AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
391                 }
392
393         } else {
394                 snprintf( textbuf, textlen,
395                         "modify/increment: %s: increment not supported for value syntax %s",
396                         mod->sm_desc->ad_cname.bv_val,
397                         a->a_desc->ad_type->sat_syntax_oid );
398                 return LDAP_CONSTRAINT_VIOLATION;
399         }
400
401         return LDAP_SUCCESS;
402 }
403
404 void
405 slap_mod_free(
406         Modification    *mod,
407         int                             freeit )
408 {
409         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
410         mod->sm_values = NULL;
411
412         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
413         mod->sm_nvalues = NULL;
414
415         if( freeit ) free( mod );
416 }
417
418 void
419 slap_mods_free(
420     Modifications       *ml )
421 {
422         Modifications *next;
423
424         for ( ; ml != NULL; ml = next ) {
425                 next = ml->sml_next;
426
427                 slap_mod_free( &ml->sml_mod, 0 );
428                 free( ml );
429         }
430 }
431