1 /* modify.c - ldbm backend modify routine */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
17 #include "back-ldbm.h"
18 #include "proto-back-ldbm.h"
20 static int add_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
21 static int delete_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
22 static int replace_values LDAP_P(( Entry *e, Modification *mod, char *dn ));
24 /* We need this function because of LDAP modrdn. If we do not
25 * add this there would be a bunch of code replication here
26 * and there and of course the likelihood of bugs increases.
27 * Juan C. Gomez (gomez@engr.sgi.com) 05/18/99
30 int ldbm_modify_internal(
35 Modifications *modlist,
43 Attribute *save_attrs;
46 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
47 "ldbm_modify_internal: %s\n", dn ));
49 Debug(LDAP_DEBUG_TRACE, "ldbm_modify_internal:\n", 0, 0, 0);
53 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
54 return LDAP_INSUFFICIENT_ACCESS;
57 save_attrs = e->e_attrs;
58 e->e_attrs = attrs_dup( e->e_attrs );
60 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
63 switch ( mod->sm_op ) {
66 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
67 "ldbm_modify_internal: add\n" ));
69 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: add\n", 0, 0, 0);
72 err = add_values( e, mod, op->o_ndn );
74 if( err != LDAP_SUCCESS ) {
75 *text = "modify: add values failed";
77 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
78 "ldbm_modify_internal: failed %d (%s)\n",
81 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
89 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
90 "ldbm_modify_internal: delete\n" ));
92 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: delete\n", 0, 0, 0);
95 err = delete_values( e, mod, op->o_ndn );
96 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
97 if( err != LDAP_SUCCESS ) {
98 *text = "modify: delete values failed";
100 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
101 "ldbm_modify_internal: failed %d (%s)\n", err, *text ));
103 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
109 case LDAP_MOD_REPLACE:
111 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
112 "ldbm_modify_internal: replace\n" ));
114 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: replace\n", 0, 0, 0);
117 err = replace_values( e, mod, op->o_ndn );
118 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
119 if( err != LDAP_SUCCESS ) {
120 *text = "modify: replace values failed";
122 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
123 "ldbm_modify_internal: failed %d (%s)\n", err, *text ));
125 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
132 case SLAP_MOD_SOFTADD:
134 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
135 "ldbm_modify_internal: softadd\n" ));
137 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: softadd\n", 0, 0, 0);
140 /* Avoid problems in index_add_mods()
141 * We need to add index if necessary.
143 mod->sm_op = LDAP_MOD_ADD;
144 err = add_values( e, mod, op->o_ndn );
146 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
150 if( err != LDAP_SUCCESS ) {
151 *text = "modify: (soft)add values failed";
153 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
154 "ldbm_modify_internal: failed %d (%s)\n", err, *text ));
156 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
165 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
166 "ldbm_modify_internal: invalid op %d\n", mod->sm_op ));
168 Debug(LDAP_DEBUG_ANY, "ldbm_modify_internal: invalid op %d\n",
173 *text = "Invalid modify operation";
175 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
176 "ldbm_modify_internal: %d (%s)\n", err, *text ));
178 Debug(LDAP_DEBUG_ARGS, "ldbm_modify_internal: %d %s\n",
184 if ( err != LDAP_SUCCESS ) {
185 attrs_free( e->e_attrs );
186 e->e_attrs = save_attrs;
187 /* unlock entry, delete from cache */
192 /* check for abandon */
193 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
194 if ( op->o_abandon ) {
195 attrs_free( e->e_attrs );
196 e->e_attrs = save_attrs;
197 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
198 return SLAPD_ABANDON;
200 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
202 /* check that the entry still obeys the schema */
203 rc = entry_schema_check( e, save_attrs, text );
204 if ( rc != LDAP_SUCCESS ) {
205 attrs_free( e->e_attrs );
206 e->e_attrs = save_attrs;
208 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
209 "ldbm_modify_internal: entry failed schema check: %s\n",
212 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
219 /* check for abandon */
220 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
221 if ( op->o_abandon ) {
222 attrs_free( e->e_attrs );
223 e->e_attrs = save_attrs;
224 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
225 return SLAPD_ABANDON;
227 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
229 /* delete indices for old attributes */
230 index_entry_del( be, e, save_attrs);
232 /* add indices for new attributes */
233 index_entry_add( be, e, e->e_attrs);
235 attrs_free( save_attrs );
248 Modifications *modlist
252 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
255 int manageDSAit = get_manageDSAit( op );
256 const char *text = NULL;
259 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
260 "ldbm_back_modify: enter\n" ));
262 Debug(LDAP_DEBUG_ARGS, "ldbm_back_modify:\n", 0, 0, 0);
266 /* acquire and lock entry */
267 if ( (e = dn2entry_w( be, ndn, &matched )) == NULL ) {
268 char* matched_dn = NULL;
269 struct berval **refs = NULL;
271 if ( matched != NULL ) {
272 matched_dn = ch_strdup( matched->e_dn );
273 refs = is_entry_referral( matched )
274 ? get_entry_referrals( be, conn, op, matched )
276 cache_return_entry_r( &li->li_cache, matched );
278 refs = default_referral;
281 send_ldap_result( conn, op, LDAP_REFERRAL,
282 matched_dn, NULL, refs, NULL );
284 if ( matched != NULL ) {
285 ber_bvecfree( refs );
292 if ( !manageDSAit && is_entry_referral( e ) ) {
293 /* parent is a referral, don't allow add */
294 /* parent is an alias, don't allow add */
295 struct berval **refs = get_entry_referrals( be,
299 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
300 "ldbm_back_modify: entry (%s) is referral\n", ndn ));
302 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
307 send_ldap_result( conn, op, LDAP_REFERRAL,
308 e->e_dn, NULL, refs, NULL );
310 ber_bvecfree( refs );
315 /* Modify the entry */
316 rc = ldbm_modify_internal( be, conn, op, ndn, modlist, e, &text );
318 if( rc != LDAP_SUCCESS ) {
319 if( rc != SLAPD_ABANDON ) {
320 send_ldap_result( conn, op, rc,
321 NULL, text, NULL, NULL );
327 /* change the entry itself */
328 if ( id2entry_add( be, e ) != 0 ) {
329 send_ldap_result( conn, op, LDAP_OTHER,
330 NULL, "id2entry failure", NULL, NULL );
334 send_ldap_result( conn, op, LDAP_SUCCESS,
335 NULL, NULL, NULL, NULL );
337 cache_return_entry_w( &li->li_cache, e );
341 cache_return_entry_w( &li->li_cache, e );
355 /* char *desc = mod->sm_desc->ad_cname->bv_val; */
356 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
358 a = attr_find( e->e_attrs, mod->sm_desc );
360 /* check if the values we're adding already exist */
362 if( mr == NULL || !mr->smr_match ) {
363 /* do not allow add of additional attribute
364 if no equality rule exists */
365 return LDAP_INAPPROPRIATE_MATCHING;
368 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
371 const char *text = NULL;
372 struct berval *asserted;
374 rc = value_normalize( mod->sm_desc,
380 if( rc != LDAP_SUCCESS ) return rc;
382 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
384 int rc = value_match( &match, mod->sm_desc, mr,
385 SLAP_MR_MODIFY_MATCHING,
386 a->a_vals[j], asserted, &text );
388 if( rc == LDAP_SUCCESS && match == 0 ) {
389 ber_bvfree( asserted );
390 return LDAP_TYPE_OR_VALUE_EXISTS;
394 ber_bvfree( asserted );
399 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
400 /* this should return result return of attr_merge */
416 char *desc = mod->sm_desc->ad_cname->bv_val;
417 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
419 /* delete the entire attribute */
420 if ( mod->sm_bvalues == NULL ) {
422 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
423 "delete_values: removing entire attribute %s\n", desc ));
425 Debug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
429 return( attr_delete( &e->e_attrs, mod->sm_desc ) ?
430 LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS );
433 if( mr == NULL || !mr->smr_match ) {
434 /* disallow specific attributes from being deleted if
436 return LDAP_INAPPROPRIATE_MATCHING;
439 /* delete specific values - find the attribute first */
440 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
442 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
443 "ldap_modify_delete: Could not find attribute %s\n", desc ));
445 Debug( LDAP_DEBUG_ARGS, "ldap_modify_delete: "
446 "could not find attribute %s\n",
450 return( LDAP_NO_SUCH_ATTRIBUTE );
453 /* find each value to delete */
454 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
456 const char *text = NULL;
458 struct berval *asserted;
460 rc = value_normalize( mod->sm_desc,
466 if( rc != LDAP_SUCCESS ) return rc;
469 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
471 int rc = value_match( &match, mod->sm_desc, mr,
472 SLAP_MR_MODIFY_MATCHING,
473 a->a_vals[j], asserted, &text );
475 if( rc == LDAP_SUCCESS && match != 0 ) {
479 /* found a matching value */
483 ber_bvfree( a->a_vals[j] );
484 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
485 a->a_vals[k - 1] = a->a_vals[k];
487 a->a_vals[k - 1] = NULL;
492 ber_bvfree( asserted );
494 /* looked through them all w/o finding it */
497 LDAP_LOG(( "backend", LDAP_LEVEL_ARGS,
498 "delete_values: could not find value for attr %s\n", desc ));
500 Debug( LDAP_DEBUG_ARGS,
501 "ldbm_modify_delete: could not find value for attr %s\n",
505 return LDAP_NO_SUCH_ATTRIBUTE;
509 /* if no values remain, delete the entire attribute */
510 if ( a->a_vals[0] == NULL ) {
512 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
513 "delete_values: removing entire attribute %s\n", desc ));
515 Debug( LDAP_DEBUG_ARGS,
516 "removing entire attribute %s\n",
520 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
521 return LDAP_NO_SUCH_ATTRIBUTE;
535 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
537 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
541 if ( mod->sm_bvalues != NULL &&
542 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )