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++ ) {
77 /* EMPTY -- just counting 'em */;
80 pmod.sm_values = (BerVarray)ch_malloc(
81 (i + 1) * sizeof( struct berval ));
82 if ( pmod.sm_nvalues != NULL ) {
83 pmod.sm_nvalues = (BerVarray)ch_malloc(
84 (i + 1) * sizeof( struct berval ));
88 /* no normalization is done in this routine nor
89 * in the matching routines called by this routine.
90 * values are now normalized once on input to the
91 * server (whether from LDAP or from the underlying
94 for ( p = i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
97 assert( a->a_vals[0].bv_val != NULL );
98 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
99 if ( mod->sm_nvalues ) {
100 rc = ordered_value_match( &match, mod->sm_desc, mr,
101 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
102 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
103 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
104 &a->a_nvals[j], &mod->sm_nvalues[i], text );
106 rc = ordered_value_match( &match, mod->sm_desc, mr,
107 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
108 &a->a_vals[j], &mod->sm_values[i], text );
111 if ( rc == LDAP_SUCCESS && match == 0 ) {
112 /* value already exists */
113 if ( permissive ) break;
116 snprintf( textbuf, textlen,
117 "modify/%s: %s: value #%d already exists",
118 op, mod->sm_desc->ad_cname.bv_val, i );
119 return LDAP_TYPE_OR_VALUE_EXISTS;
121 } else if ( rc != LDAP_SUCCESS ) {
126 if ( permissive && match != 0 ) {
127 if ( pmod.sm_nvalues ) {
128 pmod.sm_nvalues[p] = mod->sm_nvalues[i];
130 pmod.sm_values[p++] = mod->sm_values[i];
136 /* all new values match exist */
137 ch_free( pmod.sm_values );
138 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
142 BER_BVZERO( &pmod.sm_values[p] );
143 if ( pmod.sm_nvalues ) {
144 BER_BVZERO( &pmod.sm_nvalues[p] );
150 if ( mod->sm_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
151 rc = ordered_value_add( e, mod->sm_desc, a,
152 pmod.sm_values, pmod.sm_nvalues );
154 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
157 if ( a != NULL && permissive ) {
158 ch_free( pmod.sm_values );
159 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
163 /* this should return result of attr_merge */
165 snprintf( textbuf, textlen,
166 "modify/%s: %s: merge error",
167 op, mod->sm_desc->ad_cname.bv_val );
175 modify_delete_values(
180 char *textbuf, size_t textlen )
182 return modify_delete_vindex( e, m, perm, text, textbuf, textlen, NULL );
186 modify_delete_vindex(
191 char *textbuf, size_t textlen,
194 int i, j, k, rc = LDAP_SUCCESS;
196 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
201 * If permissive is set, then the non-existence of an
202 * attribute is not treated as an error.
205 /* delete the entire attribute */
206 if ( mod->sm_values == NULL ) {
207 rc = attr_delete( &e->e_attrs, mod->sm_desc );
211 } else if( rc != LDAP_SUCCESS ) {
213 snprintf( textbuf, textlen,
214 "modify/delete: %s: no such attribute",
215 mod->sm_desc->ad_cname.bv_val );
216 rc = LDAP_NO_SUCH_ATTRIBUTE;
221 if( mr == NULL || !mr->smr_match ) {
222 /* disallow specific attributes from being deleted if
225 snprintf( textbuf, textlen,
226 "modify/delete: %s: no equality matching rule",
227 mod->sm_desc->ad_cname.bv_val );
228 return LDAP_INAPPROPRIATE_MATCHING;
231 /* delete specific values - find the attribute first */
232 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
237 snprintf( textbuf, textlen,
238 "modify/delete: %s: no such attribute",
239 mod->sm_desc->ad_cname.bv_val );
240 return LDAP_NO_SUCH_ATTRIBUTE;
243 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
245 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
246 /* skip already deleted values */
247 if ( a->a_vals[j].bv_val == &dummy ) {
251 if( mod->sm_nvalues ) {
252 assert( a->a_nvals != NULL );
253 rc = ordered_value_match( &match, a->a_desc, mr,
254 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
255 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
256 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
257 &a->a_nvals[j], &mod->sm_nvalues[i], text );
260 assert( a->a_nvals == NULL );
262 rc = ordered_value_match( &match, a->a_desc, mr,
263 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
264 &a->a_vals[j], &mod->sm_values[i], text );
267 if ( rc != LDAP_SUCCESS ) {
269 snprintf( textbuf, textlen,
270 "%s: matching rule failed",
271 mod->sm_desc->ad_cname.bv_val );
284 /* delete value and mark it as dummy */
285 free( a->a_vals[j].bv_val );
286 a->a_vals[j].bv_val = &dummy;
287 if( a->a_nvals != a->a_vals ) {
288 free( a->a_nvals[j].bv_val );
289 a->a_nvals[j].bv_val = &dummy;
297 snprintf( textbuf, textlen,
298 "modify/delete: %s: no such value",
299 mod->sm_desc->ad_cname.bv_val );
300 rc = LDAP_NO_SUCH_ATTRIBUTE;
309 /* compact array skipping dummies */
310 for ( k = 0, j = 0; !BER_BVISNULL( &a->a_vals[k] ); k++ ) {
312 if( a->a_vals[k].bv_val == &dummy ) {
313 assert( a->a_nvals[k].bv_val == &dummy );
317 a->a_vals[ j ] = a->a_vals[ k ];
318 if (a->a_nvals != a->a_vals) {
319 a->a_nvals[ j ] = a->a_nvals[ k ];
326 BER_BVZERO( &a->a_vals[j] );
327 if (a->a_nvals != a->a_vals) {
328 BER_BVZERO( &a->a_nvals[j] );
331 /* if no values remain, delete the entire attribute */
332 if ( BER_BVISNULL( &a->a_vals[0] ) ) {
333 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
335 snprintf( textbuf, textlen,
336 "modify/delete: %s: no such attribute",
337 mod->sm_desc->ad_cname.bv_val );
338 rc = LDAP_NO_SUCH_ATTRIBUTE;
340 } else if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
341 /* For an ordered attribute, renumber the value indices */
342 ordered_value_sort( a, 1 );
351 modify_replace_values(
356 char *textbuf, size_t textlen )
358 (void) attr_delete( &e->e_attrs, mod->sm_desc );
360 if ( mod->sm_values ) {
361 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
368 modify_increment_values(
373 char *textbuf, size_t textlen )
377 a = attr_find( e->e_attrs, mod->sm_desc );
380 snprintf( textbuf, textlen,
381 "modify/increment: %s: no such attribute",
382 mod->sm_desc->ad_cname.bv_val );
383 return LDAP_NO_SUCH_ATTRIBUTE;
386 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
388 char str[sizeof(long)*3 + 2]; /* overly long */
389 long incr = atol( mod->sm_values[0].bv_val );
391 /* treat zero and errors as a no-op */
396 for( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
398 long value = atol( a->a_nvals[i].bv_val );
399 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
401 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
403 *text = "modify/increment: reallocation error";
406 a->a_nvals[i].bv_val = tmp;
407 a->a_nvals[i].bv_len = strln;
409 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
413 snprintf( textbuf, textlen,
414 "modify/increment: %s: increment not supported for value syntax %s",
415 mod->sm_desc->ad_cname.bv_val,
416 a->a_desc->ad_type->sat_syntax_oid );
417 return LDAP_CONSTRAINT_VIOLATION;
428 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
429 mod->sm_values = NULL;
431 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
432 mod->sm_nvalues = NULL;
434 if( freeit ) free( mod );
444 for ( ; ml != NULL; ml = next ) {
448 slap_mod_free( &ml->sml_mod, 0 );