2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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>
45 Modification pmod = *mod;
47 switch ( mod->sm_op ) {
51 case LDAP_MOD_REPLACE:
59 /* check if values to add exist in attribute */
60 a = attr_find( e->e_attrs, mod->sm_desc );
65 mr = mod->sm_desc->ad_type->sat_equality;
66 if( mr == NULL || !mr->smr_match ) {
67 /* do not allow add of additional attribute
68 if no equality rule exists */
70 snprintf( textbuf, textlen,
71 "modify/%s: %s: no equality matching rule",
72 op, mod->sm_desc->ad_cname.bv_val );
73 return LDAP_INAPPROPRIATE_MATCHING;
77 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
78 /* EMPTY -- just counting 'em */;
81 pmod.sm_values = (BerVarray)ch_malloc(
82 (i + 1) * sizeof( struct berval ));
83 if ( pmod.sm_nvalues != NULL ) {
84 pmod.sm_nvalues = (BerVarray)ch_malloc(
85 (i + 1) * sizeof( struct berval ));
89 /* no normalization is done in this routine nor
90 * in the matching routines called by this routine.
91 * values are now normalized once on input to the
92 * server (whether from LDAP or from the underlying
95 for ( p = i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
98 assert( a->a_vals[0].bv_val != NULL );
99 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
100 if ( mod->sm_nvalues ) {
101 rc = ordered_value_match( &match, mod->sm_desc, mr,
102 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
103 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
104 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
105 &a->a_nvals[j], &mod->sm_nvalues[i], text );
107 rc = ordered_value_match( &match, mod->sm_desc, mr,
108 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
109 &a->a_vals[j], &mod->sm_values[i], text );
112 if ( rc == LDAP_SUCCESS && match == 0 ) {
113 /* value already exists */
114 if ( permissive ) break;
117 snprintf( textbuf, textlen,
118 "modify/%s: %s: value #%d already exists",
119 op, mod->sm_desc->ad_cname.bv_val, i );
120 return LDAP_TYPE_OR_VALUE_EXISTS;
122 } else if ( rc != LDAP_SUCCESS ) {
127 if ( permissive && match != 0 ) {
128 if ( pmod.sm_nvalues ) {
129 pmod.sm_nvalues[p] = mod->sm_nvalues[i];
131 pmod.sm_values[p++] = mod->sm_values[i];
137 /* all new values match exist */
138 ch_free( pmod.sm_values );
139 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
143 BER_BVZERO( &pmod.sm_values[p] );
144 if ( pmod.sm_nvalues ) {
145 BER_BVZERO( &pmod.sm_nvalues[p] );
151 if ( mod->sm_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
152 rc = ordered_value_add( e, mod->sm_desc, a,
153 pmod.sm_values, pmod.sm_nvalues );
155 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
158 if ( a != NULL && permissive ) {
159 ch_free( pmod.sm_values );
160 if ( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
164 /* this should return result of attr_merge */
166 snprintf( textbuf, textlen,
167 "modify/%s: %s: merge error",
168 op, mod->sm_desc->ad_cname.bv_val );
176 modify_delete_values(
181 char *textbuf, size_t textlen )
183 return modify_delete_vindex( e, m, perm, text, textbuf, textlen, NULL );
187 modify_delete_vindex(
192 char *textbuf, size_t textlen,
195 int i, j, k, rc = LDAP_SUCCESS;
197 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
202 * If permissive is set, then the non-existence of an
203 * attribute is not treated as an error.
206 /* delete the entire attribute */
207 if ( mod->sm_values == NULL ) {
208 rc = attr_delete( &e->e_attrs, mod->sm_desc );
212 } else if( rc != LDAP_SUCCESS ) {
214 snprintf( textbuf, textlen,
215 "modify/delete: %s: no such attribute",
216 mod->sm_desc->ad_cname.bv_val );
217 rc = LDAP_NO_SUCH_ATTRIBUTE;
222 if( mr == NULL || !mr->smr_match ) {
223 /* disallow specific attributes from being deleted if
226 snprintf( textbuf, textlen,
227 "modify/delete: %s: no equality matching rule",
228 mod->sm_desc->ad_cname.bv_val );
229 return LDAP_INAPPROPRIATE_MATCHING;
232 /* delete specific values - find the attribute first */
233 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
238 snprintf( textbuf, textlen,
239 "modify/delete: %s: no such attribute",
240 mod->sm_desc->ad_cname.bv_val );
241 return LDAP_NO_SUCH_ATTRIBUTE;
244 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
246 for ( j = 0; !BER_BVISNULL( &a->a_vals[j] ); j++ ) {
247 /* skip already deleted values */
248 if ( a->a_vals[j].bv_val == &dummy ) {
252 if( mod->sm_nvalues ) {
253 assert( a->a_nvals != NULL );
254 rc = ordered_value_match( &match, a->a_desc, mr,
255 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
256 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
257 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
258 &a->a_nvals[j], &mod->sm_nvalues[i], text );
261 assert( a->a_nvals == NULL );
263 rc = ordered_value_match( &match, a->a_desc, mr,
264 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
265 &a->a_vals[j], &mod->sm_values[i], text );
268 if ( rc != LDAP_SUCCESS ) {
270 snprintf( textbuf, textlen,
271 "%s: matching rule failed",
272 mod->sm_desc->ad_cname.bv_val );
285 /* delete value and mark it as dummy */
286 free( a->a_vals[j].bv_val );
287 a->a_vals[j].bv_val = &dummy;
288 if( a->a_nvals != a->a_vals ) {
289 free( a->a_nvals[j].bv_val );
290 a->a_nvals[j].bv_val = &dummy;
298 snprintf( textbuf, textlen,
299 "modify/delete: %s: no such value",
300 mod->sm_desc->ad_cname.bv_val );
301 rc = LDAP_NO_SUCH_ATTRIBUTE;
310 /* compact array skipping dummies */
311 for ( k = 0, j = 0; !BER_BVISNULL( &a->a_vals[k] ); k++ ) {
313 if( a->a_vals[k].bv_val == &dummy ) {
314 assert( a->a_nvals[k].bv_val == &dummy );
318 a->a_vals[ j ] = a->a_vals[ k ];
319 if (a->a_nvals != a->a_vals) {
320 a->a_nvals[ j ] = a->a_nvals[ k ];
327 BER_BVZERO( &a->a_vals[j] );
328 if (a->a_nvals != a->a_vals) {
329 BER_BVZERO( &a->a_nvals[j] );
332 /* if no values remain, delete the entire attribute */
333 if ( BER_BVISNULL( &a->a_vals[0] ) ) {
334 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
336 snprintf( textbuf, textlen,
337 "modify/delete: %s: no such attribute",
338 mod->sm_desc->ad_cname.bv_val );
339 rc = LDAP_NO_SUCH_ATTRIBUTE;
341 } else if ( a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL ) {
342 /* For an ordered attribute, renumber the value indices */
343 ordered_value_sort( a, 1 );
352 modify_replace_values(
357 char *textbuf, size_t textlen )
359 (void) attr_delete( &e->e_attrs, mod->sm_desc );
361 if ( mod->sm_values ) {
362 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
369 modify_increment_values(
374 char *textbuf, size_t textlen )
378 a = attr_find( e->e_attrs, mod->sm_desc );
381 Modification modReplace = *mod;
383 modReplace.sm_op = LDAP_MOD_REPLACE;
385 return modify_add_values(e, &modReplace, permissive, text, textbuf, textlen);
388 snprintf( textbuf, textlen,
389 "modify/increment: %s: no such attribute",
390 mod->sm_desc->ad_cname.bv_val );
391 return LDAP_NO_SUCH_ATTRIBUTE;
395 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
397 char str[sizeof(long)*3 + 2]; /* overly long */
400 if ( lutil_atol( &incr, mod->sm_values[0].bv_val ) != 0 ) {
401 *text = "modify/increment: invalid syntax of increment";
402 return LDAP_INVALID_SYNTAX;
405 /* treat zero and errors as a no-op */
410 for( i = 0; !BER_BVISNULL( &a->a_nvals[i] ); i++ ) {
414 if ( lutil_atol( &value, a->a_nvals[i].bv_val ) != 0 ) {
415 *text = "modify/increment: invalid syntax of original value";
416 return LDAP_INVALID_SYNTAX;
418 strln = snprintf( str, sizeof(str), "%ld", value+incr );
420 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
422 *text = "modify/increment: reallocation error";
425 a->a_nvals[i].bv_val = tmp;
426 a->a_nvals[i].bv_len = strln;
428 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
432 snprintf( textbuf, textlen,
433 "modify/increment: %s: increment not supported for value syntax %s",
434 mod->sm_desc->ad_cname.bv_val,
435 a->a_desc->ad_type->sat_syntax_oid );
436 return LDAP_CONSTRAINT_VIOLATION;
447 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
448 mod->sm_values = NULL;
450 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
451 mod->sm_nvalues = NULL;
453 if( freeit ) free( mod );
463 for ( ; ml != NULL; ml = next ) {
467 slap_mod_free( &ml->sml_mod, 0 );