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>
38 char *textbuf, size_t textlen )
43 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
46 switch( mod->sm_op ) {
50 case LDAP_MOD_REPLACE:
58 a = attr_find( e->e_attrs, mod->sm_desc );
61 * With permissive set, as long as the attribute being added
62 * has the same value(s?) as the existing attribute, then the
63 * modify will succeed.
66 /* check if the values we're adding already exist */
67 if( mr == NULL || !mr->smr_match ) {
69 /* do not allow add of additional attribute
70 if no equality rule exists */
72 snprintf( textbuf, textlen,
73 "modify/%s: %s: no equality matching rule",
74 op, mod->sm_desc->ad_cname.bv_val );
75 return LDAP_INAPPROPRIATE_MATCHING;
78 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
79 /* test asserted values against existing values */
80 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
81 if ( bvmatch( &mod->sm_values[i], &a->a_vals[j] ) ) {
86 /* value exists already */
88 snprintf( textbuf, textlen,
89 "modify/%s: %s: value #%i already exists",
90 op, mod->sm_desc->ad_cname.bv_val, j );
91 return LDAP_TYPE_OR_VALUE_EXISTS;
95 if ( permissive && matched == j ) {
96 /* values already exist; do nothing */
101 } else if ( a != NULL ) {
102 /* no normalization is done in this routine nor
103 * in the matching routines called by this routine.
104 * values are now normalized once on input to the
105 * server (whether from LDAP or from the underlying
110 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
113 if( mod->sm_nvalues ) {
114 rc = value_match( &match, mod->sm_desc, mr,
115 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
116 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
117 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
118 &a->a_nvals[i], &mod->sm_nvalues[0], text );
120 rc = value_match( &match, mod->sm_desc, mr,
121 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
122 &a->a_vals[i], &mod->sm_values[0], text );
125 if( rc == LDAP_SUCCESS && match == 0 ) {
131 snprintf( textbuf, textlen,
132 "modify/%s: %s: value #0 already exists",
133 op, mod->sm_desc->ad_cname.bv_val );
134 return LDAP_TYPE_OR_VALUE_EXISTS;
137 if ( permissive && matched == i ) {
138 /* values already exist; do nothing */
144 if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
145 /* this should return result of attr_merge */
147 snprintf( textbuf, textlen,
148 "modify/%s: %s: merge error",
149 op, mod->sm_desc->ad_cname.bv_val );
157 modify_delete_values(
162 char *textbuf, size_t textlen )
164 int i, j, k, rc = LDAP_SUCCESS;
166 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
171 * If permissive is set, then the non-existence of an
172 * attribute is not treated as an error.
175 /* delete the entire attribute */
176 if ( mod->sm_values == NULL ) {
177 rc = attr_delete( &e->e_attrs, mod->sm_desc );
181 } else if( rc != LDAP_SUCCESS ) {
183 snprintf( textbuf, textlen,
184 "modify/delete: %s: no such attribute",
185 mod->sm_desc->ad_cname.bv_val );
186 rc = LDAP_NO_SUCH_ATTRIBUTE;
191 if( mr == NULL || !mr->smr_match ) {
192 /* disallow specific attributes from being deleted if
195 snprintf( textbuf, textlen,
196 "modify/delete: %s: no equality matching rule",
197 mod->sm_desc->ad_cname.bv_val );
198 return LDAP_INAPPROPRIATE_MATCHING;
201 /* delete specific values - find the attribute first */
202 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
207 snprintf( textbuf, textlen,
208 "modify/delete: %s: no such attribute",
209 mod->sm_desc->ad_cname.bv_val );
210 return LDAP_NO_SUCH_ATTRIBUTE;
213 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
215 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
217 if( mod->sm_nvalues ) {
218 assert( a->a_nvals );
219 rc = (*mr->smr_match)( &match,
220 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
221 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
222 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
223 a->a_desc->ad_type->sat_syntax,
225 &mod->sm_nvalues[i] );
228 assert( a->a_nvals == NULL );
230 rc = (*mr->smr_match)( &match,
231 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
232 a->a_desc->ad_type->sat_syntax,
234 &mod->sm_values[i] );
237 if ( rc != LDAP_SUCCESS ) {
239 snprintf( textbuf, textlen,
240 "%s: matching rule failed",
241 mod->sm_desc->ad_cname.bv_val );
251 /* delete value and mark it as dummy */
252 free( a->a_vals[j].bv_val );
253 a->a_vals[j].bv_val = &dummy;
254 if( a->a_nvals != a->a_vals ) {
255 free( a->a_nvals[j].bv_val );
256 a->a_nvals[j].bv_val = &dummy;
264 snprintf( textbuf, textlen,
265 "modify/delete: %s: no such value",
266 mod->sm_desc->ad_cname.bv_val );
267 rc = LDAP_NO_SUCH_ATTRIBUTE;
276 /* compact array skipping dummies */
277 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
279 if( a->a_vals[k].bv_val == &dummy ) {
280 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
284 a->a_vals[ j ] = a->a_vals[ k ];
285 if (a->a_nvals != a->a_vals) {
286 a->a_nvals[ j ] = a->a_nvals[ k ];
293 a->a_vals[j].bv_val = NULL;
294 if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
296 /* if no values remain, delete the entire attribute */
297 if ( a->a_vals[0].bv_val == NULL ) {
298 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
300 snprintf( textbuf, textlen,
301 "modify/delete: %s: no such attribute",
302 mod->sm_desc->ad_cname.bv_val );
303 rc = LDAP_NO_SUCH_ATTRIBUTE;
313 modify_replace_values(
318 char *textbuf, size_t textlen )
320 (void) attr_delete( &e->e_attrs, mod->sm_desc );
322 if ( mod->sm_values ) {
323 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
330 modify_increment_values(
335 char *textbuf, size_t textlen )
339 a = attr_find( e->e_attrs, mod->sm_desc );
342 snprintf( textbuf, textlen,
343 "modify/increment: %s: no such attribute",
344 mod->sm_desc->ad_cname.bv_val );
345 return LDAP_NO_SUCH_ATTRIBUTE;
348 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
350 char str[sizeof(long)*3 + 2]; /* overly long */
351 long incr = atol( mod->sm_values[0].bv_val );
353 /* treat zero and errors as a no-op */
358 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
360 long value = atol( a->a_nvals[i].bv_val );
361 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
363 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
365 *text = "modify/increment: reallocation error";
368 a->a_nvals[i].bv_val = tmp;
369 a->a_nvals[i].bv_len = strln;
371 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
375 snprintf( textbuf, textlen,
376 "modify/increment: %s: increment not supported for value syntax %s",
377 mod->sm_desc->ad_cname.bv_val,
378 a->a_desc->ad_type->sat_syntax_oid );
379 return LDAP_CONSTRAINT_VIOLATION;
390 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
391 mod->sm_values = NULL;
393 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
394 mod->sm_nvalues = NULL;
396 if( freeit ) free( mod );
405 for ( ; ml != NULL; ml = next ) {
408 slap_mod_free( &ml->sml_mod, 0 );