2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
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.
28 #include <ac/string.h>
44 Modification pmod = *mod;
46 switch ( mod->sm_op ) {
50 case LDAP_MOD_REPLACE:
58 /* check if values to add exist in attribute */
59 a = attr_find( e->e_attrs, mod->sm_desc );
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 */
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;
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 ) );
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
90 for ( p = i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
93 assert( a->a_vals[0].bv_val != NULL );
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 );
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 );
107 if ( rc == LDAP_SUCCESS && match == 0 ) {
108 /* value already exists */
109 if ( permissive ) break;
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;
117 } else if ( rc != LDAP_SUCCESS ) {
122 if ( permissive && match != 0 ) {
123 if ( pmod.sm_nvalues ) {
124 pmod.sm_nvalues[p] = mod->sm_nvalues[i];
126 pmod.sm_values[p++] = mod->sm_values[i];
132 /* all new values match exist */
133 ch_free( pmod.sm_values );
134 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
138 BER_BVZERO( &pmod.sm_values[p] );
139 if ( pmod.sm_nvalues ) {
140 BER_BVZERO( &pmod.sm_nvalues[p] );
146 if ( mod->sm_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
147 rc = ordered_value_add( e, mod->sm_desc, a,
148 pmod.sm_values, pmod.sm_nvalues );
150 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
153 if ( a != NULL && permissive ) {
154 ch_free( pmod.sm_values );
155 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
159 /* this should return result of attr_merge */
161 snprintf( textbuf, textlen,
162 "modify/%s: %s: merge error",
163 op, mod->sm_desc->ad_cname.bv_val );
171 modify_delete_values(
176 char *textbuf, size_t textlen )
178 return modify_delete_vindex( e, m, perm, text, textbuf, textlen, NULL );
182 modify_delete_vindex(
187 char *textbuf, size_t textlen,
190 int i, j, k, rc = LDAP_SUCCESS;
192 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
197 * If permissive is set, then the non-existence of an
198 * attribute is not treated as an error.
201 /* delete the entire attribute */
202 if ( mod->sm_values == NULL ) {
203 rc = attr_delete( &e->e_attrs, mod->sm_desc );
207 } else if( rc != LDAP_SUCCESS ) {
209 snprintf( textbuf, textlen,
210 "modify/delete: %s: no such attribute",
211 mod->sm_desc->ad_cname.bv_val );
212 rc = LDAP_NO_SUCH_ATTRIBUTE;
217 if( mr == NULL || !mr->smr_match ) {
218 /* disallow specific attributes from being deleted if
221 snprintf( textbuf, textlen,
222 "modify/delete: %s: no equality matching rule",
223 mod->sm_desc->ad_cname.bv_val );
224 return LDAP_INAPPROPRIATE_MATCHING;
227 /* delete specific values - find the attribute first */
228 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
233 snprintf( textbuf, textlen,
234 "modify/delete: %s: no such attribute",
235 mod->sm_desc->ad_cname.bv_val );
236 return LDAP_NO_SUCH_ATTRIBUTE;
239 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
241 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
242 /* skip already deleted values */
243 if ( a->a_vals[j].bv_val == &dummy ) {
247 if( mod->sm_nvalues ) {
248 assert( a->a_nvals != NULL );
249 rc = ordered_value_match( &match, a->a_desc, mr,
250 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
251 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
252 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
253 &a->a_nvals[j], &mod->sm_nvalues[i], text );
256 assert( a->a_nvals == NULL );
258 rc = ordered_value_match( &match, a->a_desc, mr,
259 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
260 &a->a_vals[j], &mod->sm_values[i], text );
263 if ( rc != LDAP_SUCCESS ) {
265 snprintf( textbuf, textlen,
266 "%s: matching rule failed",
267 mod->sm_desc->ad_cname.bv_val );
280 /* delete value and mark it as dummy */
281 free( a->a_vals[j].bv_val );
282 a->a_vals[j].bv_val = &dummy;
283 if( a->a_nvals != a->a_vals ) {
284 free( a->a_nvals[j].bv_val );
285 a->a_nvals[j].bv_val = &dummy;
293 snprintf( textbuf, textlen,
294 "modify/delete: %s: no such value",
295 mod->sm_desc->ad_cname.bv_val );
296 rc = LDAP_NO_SUCH_ATTRIBUTE;
305 /* compact array skipping dummies */
306 for ( k = 0, j = 0; !BER_BVISNULL( &a->a_vals[k] ); k++ ) {
308 if( a->a_vals[k].bv_val == &dummy ) {
309 assert( a->a_nvals[k].bv_val == &dummy );
313 a->a_vals[ j ] = a->a_vals[ k ];
314 if (a->a_nvals != a->a_vals) {
315 a->a_nvals[ j ] = a->a_nvals[ k ];
322 BER_BVZERO( &a->a_vals[j] );
323 if (a->a_nvals != a->a_vals) {
324 BER_BVZERO( &a->a_nvals[j] );
327 /* if no values remain, delete the entire attribute */
328 if ( BER_BVISNULL( &a->a_vals[0] ) ) {
329 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
331 snprintf( textbuf, textlen,
332 "modify/delete: %s: no such attribute",
333 mod->sm_desc->ad_cname.bv_val );
334 rc = LDAP_NO_SUCH_ATTRIBUTE;
336 } else if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
337 /* For an ordered attribute, renumber the value indices */
338 ordered_value_sort( a, 1 );
347 modify_replace_values(
352 char *textbuf, size_t textlen )
354 (void) attr_delete( &e->e_attrs, mod->sm_desc );
356 if ( mod->sm_values ) {
357 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
364 modify_increment_values(
369 char *textbuf, size_t textlen )
373 a = attr_find( e->e_attrs, mod->sm_desc );
376 snprintf( textbuf, textlen,
377 "modify/increment: %s: no such attribute",
378 mod->sm_desc->ad_cname.bv_val );
379 return LDAP_NO_SUCH_ATTRIBUTE;
382 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
384 char str[sizeof(long)*3 + 2]; /* overly long */
385 long incr = atol( mod->sm_values[0].bv_val );
387 /* treat zero and errors as a no-op */
392 for( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
394 long value = atol( a->a_nvals[i].bv_val );
395 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
397 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
399 *text = "modify/increment: reallocation error";
402 a->a_nvals[i].bv_val = tmp;
403 a->a_nvals[i].bv_len = strln;
405 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
409 snprintf( textbuf, textlen,
410 "modify/increment: %s: increment not supported for value syntax %s",
411 mod->sm_desc->ad_cname.bv_val,
412 a->a_desc->ad_type->sat_syntax_oid );
413 return LDAP_CONSTRAINT_VIOLATION;
424 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
425 mod->sm_values = NULL;
427 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
428 mod->sm_nvalues = NULL;
430 if( freeit ) free( mod );
440 for ( ; ml != NULL; ml = next ) {
444 slap_mod_free( &ml->sml_mod, 0 );