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 Modification pmod = *mod;
45 switch( mod->sm_op ) {
49 case LDAP_MOD_REPLACE:
57 a = attr_find( e->e_attrs, mod->sm_desc );
58 if( a != NULL ) { /* check if values to add exist in attribute */
62 mr = mod->sm_desc->ad_type->sat_equality;
63 if( mr == NULL || !mr->smr_match ) {
64 /* do not allow add of additional attribute
65 if no equality rule exists */
67 snprintf( textbuf, textlen,
68 "modify/%s: %s: no equality matching rule",
69 op, mod->sm_desc->ad_cname.bv_val );
70 return LDAP_INAPPROPRIATE_MATCHING;
74 for ( i=0; mod->sm_values[i].bv_val; i++ ) /* count 'em */;
75 pmod.sm_values = (BerVarray) ch_malloc( i*sizeof( struct berval ));
76 if( pmod.sm_nvalues != NULL ) {
77 pmod.sm_nvalues = (BerVarray) ch_malloc(
78 i*sizeof( struct berval ));
82 /* no normalization is done in this routine nor
83 * in the matching routines called by this routine.
84 * values are now normalized once on input to the
85 * server (whether from LDAP or from the underlying
88 for ( p=i=0; mod->sm_values[i].bv_val; i++ ) {
90 assert( a->a_vals[0].bv_val );
91 for ( j=0; a->a_vals[j].bv_val; j++ ) {
92 if( mod->sm_nvalues ) {
93 rc = value_match( &match, mod->sm_desc, mr,
94 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
95 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
96 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
97 &a->a_nvals[j], &mod->sm_nvalues[i], text );
99 rc = value_match( &match, mod->sm_desc, mr,
100 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
101 &a->a_vals[j], &mod->sm_values[i], text );
104 if( rc == LDAP_SUCCESS && match == 0 ) {
105 if( permissive ) break;
107 /* value already exists */
109 snprintf( textbuf, textlen,
110 "modify/%s: %s: value #%d already exists",
111 op, mod->sm_desc->ad_cname.bv_val, i );
112 return LDAP_TYPE_OR_VALUE_EXISTS;
114 } else if ( rc != LDAP_SUCCESS ) {
119 if( permissive && !match ) {
120 if( pmod.sm_nvalues ) {
121 pmod.sm_nvalues[p] = mod->sm_nvalues[i];
123 pmod.sm_values[p++] = mod->sm_values[i];
127 if( permissive && p == 0 ) {
128 /* all new values match exist */
129 ch_free( pmod.sm_values );
130 if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
136 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
138 if( a != NULL && permissive ) {
139 ch_free( pmod.sm_values );
140 if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
144 /* this should return result of attr_merge */
146 snprintf( textbuf, textlen,
147 "modify/%s: %s: merge error",
148 op, mod->sm_desc->ad_cname.bv_val );
156 modify_delete_values(
161 char *textbuf, size_t textlen )
163 int i, j, k, rc = LDAP_SUCCESS;
165 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
170 * If permissive is set, then the non-existence of an
171 * attribute is not treated as an error.
174 /* delete the entire attribute */
175 if ( mod->sm_values == NULL ) {
176 rc = attr_delete( &e->e_attrs, mod->sm_desc );
180 } else if( rc != LDAP_SUCCESS ) {
182 snprintf( textbuf, textlen,
183 "modify/delete: %s: no such attribute",
184 mod->sm_desc->ad_cname.bv_val );
185 rc = LDAP_NO_SUCH_ATTRIBUTE;
190 if( mr == NULL || !mr->smr_match ) {
191 /* disallow specific attributes from being deleted if
194 snprintf( textbuf, textlen,
195 "modify/delete: %s: no equality matching rule",
196 mod->sm_desc->ad_cname.bv_val );
197 return LDAP_INAPPROPRIATE_MATCHING;
200 /* delete specific values - find the attribute first */
201 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
206 snprintf( textbuf, textlen,
207 "modify/delete: %s: no such attribute",
208 mod->sm_desc->ad_cname.bv_val );
209 return LDAP_NO_SUCH_ATTRIBUTE;
212 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
214 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
216 if( mod->sm_nvalues ) {
217 assert( a->a_nvals );
218 rc = (*mr->smr_match)( &match,
219 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
220 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
221 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
222 a->a_desc->ad_type->sat_syntax,
224 &mod->sm_nvalues[i] );
227 assert( a->a_nvals == NULL );
229 rc = (*mr->smr_match)( &match,
230 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
231 a->a_desc->ad_type->sat_syntax,
233 &mod->sm_values[i] );
236 if ( rc != LDAP_SUCCESS ) {
238 snprintf( textbuf, textlen,
239 "%s: matching rule failed",
240 mod->sm_desc->ad_cname.bv_val );
250 /* delete value and mark it as dummy */
251 free( a->a_vals[j].bv_val );
252 a->a_vals[j].bv_val = &dummy;
253 if( a->a_nvals != a->a_vals ) {
254 free( a->a_nvals[j].bv_val );
255 a->a_nvals[j].bv_val = &dummy;
263 snprintf( textbuf, textlen,
264 "modify/delete: %s: no such value",
265 mod->sm_desc->ad_cname.bv_val );
266 rc = LDAP_NO_SUCH_ATTRIBUTE;
275 /* compact array skipping dummies */
276 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
278 if( a->a_vals[k].bv_val == &dummy ) {
279 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
283 a->a_vals[ j ] = a->a_vals[ k ];
284 if (a->a_nvals != a->a_vals) {
285 a->a_nvals[ j ] = a->a_nvals[ k ];
292 a->a_vals[j].bv_val = NULL;
293 if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
295 /* if no values remain, delete the entire attribute */
296 if ( a->a_vals[0].bv_val == NULL ) {
297 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
299 snprintf( textbuf, textlen,
300 "modify/delete: %s: no such attribute",
301 mod->sm_desc->ad_cname.bv_val );
302 rc = LDAP_NO_SUCH_ATTRIBUTE;
312 modify_replace_values(
317 char *textbuf, size_t textlen )
319 (void) attr_delete( &e->e_attrs, mod->sm_desc );
321 if ( mod->sm_values ) {
322 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
329 modify_increment_values(
334 char *textbuf, size_t textlen )
338 a = attr_find( e->e_attrs, mod->sm_desc );
341 snprintf( textbuf, textlen,
342 "modify/increment: %s: no such attribute",
343 mod->sm_desc->ad_cname.bv_val );
344 return LDAP_NO_SUCH_ATTRIBUTE;
347 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
349 char str[sizeof(long)*3 + 2]; /* overly long */
350 long incr = atol( mod->sm_values[0].bv_val );
352 /* treat zero and errors as a no-op */
357 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
359 long value = atol( a->a_nvals[i].bv_val );
360 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
362 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
364 *text = "modify/increment: reallocation error";
367 a->a_nvals[i].bv_val = tmp;
368 a->a_nvals[i].bv_len = strln;
370 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
374 snprintf( textbuf, textlen,
375 "modify/increment: %s: increment not supported for value syntax %s",
376 mod->sm_desc->ad_cname.bv_val,
377 a->a_desc->ad_type->sat_syntax_oid );
378 return LDAP_CONSTRAINT_VIOLATION;
389 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
390 mod->sm_values = NULL;
392 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
393 mod->sm_nvalues = NULL;
395 if( freeit ) free( mod );
404 for ( ; ml != NULL; ml = next ) {
407 slap_mod_free( &ml->sml_mod, 0 );