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;
116 if( permissive && !match ) {
117 if( pmod.sm_nvalues ) {
118 pmod.sm_nvalues[p] = mod->sm_nvalues[i];
120 pmod.sm_values[p++] = mod->sm_values[i];
124 if( permissive && p == 0 ) {
125 /* all new values match exist */
126 ch_free( pmod.sm_values );
127 if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
133 rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
135 if( a != NULL && permissive ) {
136 ch_free( pmod.sm_values );
137 if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
141 /* this should return result of attr_merge */
143 snprintf( textbuf, textlen,
144 "modify/%s: %s: merge error",
145 op, mod->sm_desc->ad_cname.bv_val );
153 modify_delete_values(
158 char *textbuf, size_t textlen )
160 int i, j, k, rc = LDAP_SUCCESS;
162 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
167 * If permissive is set, then the non-existence of an
168 * attribute is not treated as an error.
171 /* delete the entire attribute */
172 if ( mod->sm_values == NULL ) {
173 rc = attr_delete( &e->e_attrs, mod->sm_desc );
177 } else if( rc != LDAP_SUCCESS ) {
179 snprintf( textbuf, textlen,
180 "modify/delete: %s: no such attribute",
181 mod->sm_desc->ad_cname.bv_val );
182 rc = LDAP_NO_SUCH_ATTRIBUTE;
187 if( mr == NULL || !mr->smr_match ) {
188 /* disallow specific attributes from being deleted if
191 snprintf( textbuf, textlen,
192 "modify/delete: %s: no equality matching rule",
193 mod->sm_desc->ad_cname.bv_val );
194 return LDAP_INAPPROPRIATE_MATCHING;
197 /* delete specific values - find the attribute first */
198 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
203 snprintf( textbuf, textlen,
204 "modify/delete: %s: no such attribute",
205 mod->sm_desc->ad_cname.bv_val );
206 return LDAP_NO_SUCH_ATTRIBUTE;
209 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
211 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
213 if( mod->sm_nvalues ) {
214 assert( a->a_nvals );
215 rc = (*mr->smr_match)( &match,
216 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
217 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
218 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
219 a->a_desc->ad_type->sat_syntax,
221 &mod->sm_nvalues[i] );
224 assert( a->a_nvals == NULL );
226 rc = (*mr->smr_match)( &match,
227 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
228 a->a_desc->ad_type->sat_syntax,
230 &mod->sm_values[i] );
233 if ( rc != LDAP_SUCCESS ) {
235 snprintf( textbuf, textlen,
236 "%s: matching rule failed",
237 mod->sm_desc->ad_cname.bv_val );
247 /* delete value and mark it as dummy */
248 free( a->a_vals[j].bv_val );
249 a->a_vals[j].bv_val = &dummy;
250 if( a->a_nvals != a->a_vals ) {
251 free( a->a_nvals[j].bv_val );
252 a->a_nvals[j].bv_val = &dummy;
260 snprintf( textbuf, textlen,
261 "modify/delete: %s: no such value",
262 mod->sm_desc->ad_cname.bv_val );
263 rc = LDAP_NO_SUCH_ATTRIBUTE;
272 /* compact array skipping dummies */
273 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
275 if( a->a_vals[k].bv_val == &dummy ) {
276 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
280 a->a_vals[ j ] = a->a_vals[ k ];
281 if (a->a_nvals != a->a_vals) {
282 a->a_nvals[ j ] = a->a_nvals[ k ];
289 a->a_vals[j].bv_val = NULL;
290 if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
292 /* if no values remain, delete the entire attribute */
293 if ( a->a_vals[0].bv_val == NULL ) {
294 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
296 snprintf( textbuf, textlen,
297 "modify/delete: %s: no such attribute",
298 mod->sm_desc->ad_cname.bv_val );
299 rc = LDAP_NO_SUCH_ATTRIBUTE;
309 modify_replace_values(
314 char *textbuf, size_t textlen )
316 (void) attr_delete( &e->e_attrs, mod->sm_desc );
318 if ( mod->sm_values ) {
319 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
326 modify_increment_values(
331 char *textbuf, size_t textlen )
335 a = attr_find( e->e_attrs, mod->sm_desc );
338 snprintf( textbuf, textlen,
339 "modify/increment: %s: no such attribute",
340 mod->sm_desc->ad_cname.bv_val );
341 return LDAP_NO_SUCH_ATTRIBUTE;
344 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
346 char str[sizeof(long)*3 + 2]; /* overly long */
347 long incr = atol( mod->sm_values[0].bv_val );
349 /* treat zero and errors as a no-op */
354 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
356 long value = atol( a->a_nvals[i].bv_val );
357 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
359 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
361 *text = "modify/increment: reallocation error";
364 a->a_nvals[i].bv_val = tmp;
365 a->a_nvals[i].bv_len = strln;
367 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
371 snprintf( textbuf, textlen,
372 "modify/increment: %s: increment not supported for value syntax %s",
373 mod->sm_desc->ad_cname.bv_val,
374 a->a_desc->ad_type->sat_syntax_oid );
375 return LDAP_CONSTRAINT_VIOLATION;
386 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
387 mod->sm_values = NULL;
389 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
390 mod->sm_nvalues = NULL;
392 if( freeit ) free( mod );
401 for ( ; ml != NULL; ml = next ) {
404 slap_mod_free( &ml->sml_mod, 0 );