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 switch( mod->sm_op ) {
47 case LDAP_MOD_REPLACE:
55 a = attr_find( e->e_attrs, mod->sm_desc );
56 if( a != NULL ) { /* check if values to add exist in attribute */
60 mr = mod->sm_desc->ad_type->sat_equality;
61 if( mr == NULL || !mr->smr_match ) {
62 /* do not allow add of additional attribute
63 if no equality rule exists */
65 snprintf( textbuf, textlen,
66 "modify/%s: %s: no equality matching rule",
67 op, mod->sm_desc->ad_cname.bv_val );
68 return LDAP_INAPPROPRIATE_MATCHING;
71 /* no normalization is done in this routine nor
72 * in the matching routines called by this routine.
73 * values are now normalized once on input to the
74 * server (whether from LDAP or from the underlying
77 for ( i=0; mod->sm_values[i].bv_val; i++ ) {
78 for ( j=0; a->a_vals[j].bv_val; j++ ) {
80 if( mod->sm_nvalues ) {
81 rc = value_match( &match, mod->sm_desc, mr,
82 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
83 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
84 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
85 &a->a_nvals[j], &mod->sm_nvalues[i], text );
87 rc = value_match( &match, mod->sm_desc, mr,
88 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
89 &a->a_vals[j], &mod->sm_values[i], text );
92 if( rc == LDAP_SUCCESS && match == 0 ) {
93 /* value already exists */
95 snprintf( textbuf, textlen,
96 "modify/%s: %s: value #%d already exists",
97 op, mod->sm_desc->ad_cname.bv_val, i );
98 return LDAP_TYPE_OR_VALUE_EXISTS;
105 if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
106 /* this should return result of attr_merge */
108 snprintf( textbuf, textlen,
109 "modify/%s: %s: merge error",
110 op, mod->sm_desc->ad_cname.bv_val );
118 modify_delete_values(
123 char *textbuf, size_t textlen )
125 int i, j, k, rc = LDAP_SUCCESS;
127 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
132 * If permissive is set, then the non-existence of an
133 * attribute is not treated as an error.
136 /* delete the entire attribute */
137 if ( mod->sm_values == NULL ) {
138 rc = attr_delete( &e->e_attrs, mod->sm_desc );
142 } else if( rc != LDAP_SUCCESS ) {
144 snprintf( textbuf, textlen,
145 "modify/delete: %s: no such attribute",
146 mod->sm_desc->ad_cname.bv_val );
147 rc = LDAP_NO_SUCH_ATTRIBUTE;
152 if( mr == NULL || !mr->smr_match ) {
153 /* disallow specific attributes from being deleted if
156 snprintf( textbuf, textlen,
157 "modify/delete: %s: no equality matching rule",
158 mod->sm_desc->ad_cname.bv_val );
159 return LDAP_INAPPROPRIATE_MATCHING;
162 /* delete specific values - find the attribute first */
163 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
168 snprintf( textbuf, textlen,
169 "modify/delete: %s: no such attribute",
170 mod->sm_desc->ad_cname.bv_val );
171 return LDAP_NO_SUCH_ATTRIBUTE;
174 for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
176 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
178 if( mod->sm_nvalues ) {
179 assert( a->a_nvals );
180 rc = (*mr->smr_match)( &match,
181 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
182 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
183 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
184 a->a_desc->ad_type->sat_syntax,
186 &mod->sm_nvalues[i] );
189 assert( a->a_nvals == NULL );
191 rc = (*mr->smr_match)( &match,
192 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
193 a->a_desc->ad_type->sat_syntax,
195 &mod->sm_values[i] );
198 if ( rc != LDAP_SUCCESS ) {
200 snprintf( textbuf, textlen,
201 "%s: matching rule failed",
202 mod->sm_desc->ad_cname.bv_val );
212 /* delete value and mark it as dummy */
213 free( a->a_vals[j].bv_val );
214 a->a_vals[j].bv_val = &dummy;
215 if( a->a_nvals != a->a_vals ) {
216 free( a->a_nvals[j].bv_val );
217 a->a_nvals[j].bv_val = &dummy;
225 snprintf( textbuf, textlen,
226 "modify/delete: %s: no such value",
227 mod->sm_desc->ad_cname.bv_val );
228 rc = LDAP_NO_SUCH_ATTRIBUTE;
237 /* compact array skipping dummies */
238 for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
240 if( a->a_vals[k].bv_val == &dummy ) {
241 assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
245 a->a_vals[ j ] = a->a_vals[ k ];
246 if (a->a_nvals != a->a_vals) {
247 a->a_nvals[ j ] = a->a_nvals[ k ];
254 a->a_vals[j].bv_val = NULL;
255 if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
257 /* if no values remain, delete the entire attribute */
258 if ( a->a_vals[0].bv_val == NULL ) {
259 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
261 snprintf( textbuf, textlen,
262 "modify/delete: %s: no such attribute",
263 mod->sm_desc->ad_cname.bv_val );
264 rc = LDAP_NO_SUCH_ATTRIBUTE;
274 modify_replace_values(
279 char *textbuf, size_t textlen )
281 (void) attr_delete( &e->e_attrs, mod->sm_desc );
283 if ( mod->sm_values ) {
284 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
291 modify_increment_values(
296 char *textbuf, size_t textlen )
300 a = attr_find( e->e_attrs, mod->sm_desc );
303 snprintf( textbuf, textlen,
304 "modify/increment: %s: no such attribute",
305 mod->sm_desc->ad_cname.bv_val );
306 return LDAP_NO_SUCH_ATTRIBUTE;
309 if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
311 char str[sizeof(long)*3 + 2]; /* overly long */
312 long incr = atol( mod->sm_values[0].bv_val );
314 /* treat zero and errors as a no-op */
319 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
321 long value = atol( a->a_nvals[i].bv_val );
322 size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
324 tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
326 *text = "modify/increment: reallocation error";
329 a->a_nvals[i].bv_val = tmp;
330 a->a_nvals[i].bv_len = strln;
332 AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
336 snprintf( textbuf, textlen,
337 "modify/increment: %s: increment not supported for value syntax %s",
338 mod->sm_desc->ad_cname.bv_val,
339 a->a_desc->ad_type->sat_syntax_oid );
340 return LDAP_CONSTRAINT_VIOLATION;
351 if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
352 mod->sm_values = NULL;
354 if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
355 mod->sm_nvalues = NULL;
357 if( freeit ) free( mod );
366 for ( ; ml != NULL; ml = next ) {
369 slap_mod_free( &ml->sml_mod, 0 );