2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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>
33 modify_check_duplicates(
34 AttributeDescription *ad,
40 char *textbuf, size_t textlen )
42 int i, j, numvals = 0, nummods,
43 rc = LDAP_SUCCESS, matched;
44 /* this function is no longer used */
54 char *textbuf, size_t textlen )
59 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
62 switch( mod->sm_op ) {
66 case LDAP_MOD_REPLACE:
74 a = attr_find( e->e_attrs, mod->sm_desc );
77 * With permissive set, as long as the attribute being added
78 * has the same value(s?) as the existing attribute, then the
79 * modify will succeed.
82 /* check if the values we're adding already exist */
83 if( mr == NULL || !mr->smr_match ) {
85 /* do not allow add of additional attribute
86 if no equality rule exists */
88 snprintf( textbuf, textlen,
89 "modify/%s: %s: no equality matching rule",
90 op, mod->sm_desc->ad_cname.bv_val );
91 return LDAP_INAPPROPRIATE_MATCHING;
94 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
95 /* test asserted values against existing values */
97 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
98 if ( bvmatch( &mod->sm_bvalues[i], &a->a_vals[j] ) ) {
103 /* value exists already */
105 snprintf( textbuf, textlen,
106 "modify/%s: %s: value #%i already exists",
107 op, mod->sm_desc->ad_cname.bv_val, j );
108 return LDAP_TYPE_OR_VALUE_EXISTS;
111 if ( permissive && matched == j ) {
112 /* values already exist; do nothing */
117 /* test asserted values against themselves */
118 for( j = 0; j < i; j++ ) {
119 if ( bvmatch( &mod->sm_bvalues[i], &mod->sm_bvalues[j] ) ) {
120 /* value exists already */
122 snprintf( textbuf, textlen,
123 "modify/%s: %s: value #%i already exists",
124 op, mod->sm_desc->ad_cname.bv_val, j );
125 return LDAP_TYPE_OR_VALUE_EXISTS;
131 /* no normalization is done in this routine nor
132 * in the matching routines called by this routine.
133 * values are now normalized once on input to the
134 * server (whether from LDAP or from the underlying
136 * This should outperform the old code. No numbers
142 if ( mod->sm_bvalues[1].bv_val == 0 ) {
146 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
149 if( mod->sm_nvalues ) {
150 rc = value_match( &match, mod->sm_desc, mr,
152 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
153 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
154 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
160 rc = value_match( &match, mod->sm_desc, mr,
162 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
169 if( rc == LDAP_SUCCESS && match == 0 ) {
175 snprintf( textbuf, textlen,
176 "modify/%s: %s: value #0 already exists",
177 op, mod->sm_desc->ad_cname.bv_val );
178 return LDAP_TYPE_OR_VALUE_EXISTS;
181 if ( permissive && matched == i ) {
182 /* values already exist; do nothing */
188 rc = modify_check_duplicates( mod->sm_desc, mr,
189 a ? a->a_vals : NULL, mod->sm_bvalues,
190 permissive, text, textbuf, textlen );
192 if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
196 if ( rc != LDAP_SUCCESS ) {
203 if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
204 /* this should return result of attr_merge */
206 snprintf( textbuf, textlen,
207 "modify/%s: %s: merge error",
208 op, mod->sm_desc->ad_cname.bv_val );
216 modify_delete_values(
221 char *textbuf, size_t textlen
224 int i, j, k, rc = LDAP_SUCCESS;
226 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
231 * If permissive is set, then the non-existence of an
232 * attribute is not treated as an error.
235 /* delete the entire attribute */
236 if ( mod->sm_bvalues == NULL ) {
237 rc = attr_delete( &e->e_attrs, mod->sm_desc );
241 } else if( rc != LDAP_SUCCESS ) {
243 snprintf( textbuf, textlen,
244 "modify/delete: %s: no such attribute",
245 mod->sm_desc->ad_cname.bv_val );
246 rc = LDAP_NO_SUCH_ATTRIBUTE;
251 if( mr == NULL || !mr->smr_match ) {
252 /* disallow specific attributes from being deleted if
255 snprintf( textbuf, textlen,
256 "modify/delete: %s: no equality matching rule",
257 mod->sm_desc->ad_cname.bv_val );
258 return LDAP_INAPPROPRIATE_MATCHING;
261 /* delete specific values - find the attribute first */
262 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
267 snprintf( textbuf, textlen,
268 "modify/delete: %s: no such attribute",
269 mod->sm_desc->ad_cname.bv_val );
270 return LDAP_NO_SUCH_ATTRIBUTE;
274 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
276 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
278 if( mod->sm_nvalues ) {
279 assert( a->a_nvals );
280 rc = (*mr->smr_match)( &match,
281 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
282 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
283 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
284 a->a_desc->ad_type->sat_syntax,
286 &mod->sm_nvalues[i] );
289 assert( a->a_nvals == NULL );
291 rc = (*mr->smr_match)( &match,
292 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
293 a->a_desc->ad_type->sat_syntax,
295 &mod->sm_values[i] );
298 if ( rc != LDAP_SUCCESS ) {
300 snprintf( textbuf, textlen,
301 "%s: matching rule failed",
302 mod->sm_desc->ad_cname.bv_val );
312 /* delete value and mark it as dummy */
313 free( a->a_vals[j].bv_val );
314 a->a_vals[j].bv_val = &dummy;
315 if( a->a_nvals != a->a_vals ) {
316 free( a->a_nvals[j].bv_val );
317 a->a_nvals[j].bv_val = &dummy;
325 snprintf( textbuf, textlen,
326 "modify/delete: %s: no such value",
327 mod->sm_desc->ad_cname.bv_val );
328 rc = LDAP_NO_SUCH_ATTRIBUTE;
337 /* compact array skipping dummies */
338 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
340 if( a->a_vals[k].bv_val == &dummy ) {
341 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
345 a->a_vals[ j ] = a->a_vals[ k ];
346 if (a->a_nvals != a->a_vals) {
347 a->a_nvals[ j ] = a->a_nvals[ k ];
354 a->a_vals[j].bv_val = NULL;
355 if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
357 /* if no values remain, delete the entire attribute */
358 if ( a->a_vals[0].bv_val == NULL ) {
359 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
361 snprintf( textbuf, textlen,
362 "modify/delete: %s: no such attribute",
363 mod->sm_desc->ad_cname.bv_val );
364 rc = LDAP_NO_SUCH_ATTRIBUTE;
374 modify_replace_values(
379 char *textbuf, size_t textlen )
381 (void) attr_delete( &e->e_attrs, mod->sm_desc );
383 if ( mod->sm_bvalues ) {
384 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
391 modify_increment_values(
396 char *textbuf, size_t textlen )
400 a = attr_find( e->e_attrs, mod->sm_desc );
403 snprintf( textbuf, textlen,
404 "modify/increment: %s: no such attribute",
405 mod->sm_desc->ad_cname.bv_val );
406 return LDAP_NO_SUCH_ATTRIBUTE;
410 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
412 char str[sizeof(long)*3 + 2]; /* overly long */
413 long incr = atol( mod->sm_bvalues[0].bv_val );
415 /* treat zero and errors as a no-op */
420 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
422 long value = atol( a->a_nvals[i].bv_val );
423 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
425 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
427 *text = "modify/increment: reallocation error";
430 a->a_nvals[i].bv_val = tmp;
431 a->a_nvals[i].bv_len = strln;
433 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
437 snprintf( textbuf, textlen,
438 "modify/increment: %s: increment not supported for value syntax %s",
439 mod->sm_desc->ad_cname.bv_val,
440 a->a_desc->ad_type->sat_syntax_oid );
441 return LDAP_CONSTRAINT_VIOLATION;
452 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
453 mod->sm_values = NULL;
455 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
456 mod->sm_nvalues = NULL;
458 if( freeit ) free( mod );
467 for ( ; ml != NULL; ml = next ) {
470 slap_mod_free( &ml->sml_mod, 0 );