1 /* modify.c - bdb backend modify routine */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 static int add_values( Entry *e, Modification *mod, char *dn );
18 static int delete_values( Entry *e, Modification *mod, char *dn );
19 static int replace_values( Entry *e, Modification *mod, char *dn );
21 int bdb_modify_internal(
26 Modifications *modlist,
33 Attribute *save_attrs;
35 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
38 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
39 return LDAP_INSUFFICIENT_ACCESS;
42 save_attrs = e->e_attrs;
43 e->e_attrs = attrs_dup( e->e_attrs );
45 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
48 switch ( mod->sm_op ) {
50 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
51 err = add_values( e, mod, op->o_ndn );
53 if( err != LDAP_SUCCESS ) {
54 *text = "modify: add values failed";
55 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
61 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
62 err = delete_values( e, mod, op->o_ndn );
63 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
64 if( err != LDAP_SUCCESS ) {
65 *text = "modify: delete values failed";
66 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
71 case LDAP_MOD_REPLACE:
72 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
73 err = replace_values( e, mod, op->o_ndn );
74 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
75 if( err != LDAP_SUCCESS ) {
76 *text = "modify: replace values failed";
77 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
82 case SLAP_MOD_SOFTADD:
83 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
84 /* Avoid problems in index_add_mods()
85 * We need to add index if necessary.
87 mod->sm_op = LDAP_MOD_ADD;
88 err = add_values( e, mod, op->o_ndn );
90 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
94 if( err != LDAP_SUCCESS ) {
95 *text = "modify: (soft)add values failed";
96 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
102 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
104 *text = "Invalid modify operation";
106 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
110 if ( err != LDAP_SUCCESS ) {
111 attrs_free( e->e_attrs );
112 e->e_attrs = save_attrs;
113 /* unlock entry, delete from cache */
118 /* check that the entry still obeys the schema */
119 rc = entry_schema_check( e, save_attrs, text );
120 if ( rc != LDAP_SUCCESS ) {
121 attrs_free( e->e_attrs );
122 e->e_attrs = save_attrs;
123 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
129 /* delete indices for old attributes */
130 rc = index_entry_del( be, tid, e, save_attrs);
132 /* add indices for new attributes */
133 rc = index_entry_add( be, tid, e, e->e_attrs);
136 attrs_free( save_attrs );
149 Modifications *modlist )
151 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
155 int manageDSAit = get_manageDSAit( op );
156 const char *text = NULL;
158 struct bdb_op_info opinfo;
160 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
163 retry: /* transaction retry */
164 Debug(LDAP_DEBUG_TRACE,
165 "bdb_modify: retrying...\n", 0, 0, 0);
166 rc = txn_abort( ltid );
168 op->o_private = NULL;
171 text = "internal error";
176 /* begin transaction */
177 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
180 Debug( LDAP_DEBUG_TRACE,
181 "bdb_modify: txn_begin failed: %s (%d)\n",
182 db_strerror(rc), rc, 0 );
184 text = "internal error";
189 opinfo.boi_txn = ltid;
191 op->o_private = &opinfo;
194 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
197 Debug( LDAP_DEBUG_TRACE,
198 "bdb_modify: dn2entry failed (%d)\n",
201 case DB_LOCK_DEADLOCK:
202 case DB_LOCK_NOTGRANTED:
209 text = "internal error";
213 /* acquire and lock entry */
215 char* matched_dn = NULL;
216 struct berval **refs = NULL;
218 if ( matched != NULL ) {
219 matched_dn = ch_strdup( matched->e_dn );
220 refs = is_entry_referral( matched )
221 ? get_entry_referrals( be, conn, op, matched )
223 bdb_entry_return( be, matched );
227 refs = default_referral;
230 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
231 matched_dn, NULL, refs, NULL );
233 if ( matched != NULL ) {
234 ber_bvecfree( refs );
241 if ( !manageDSAit && is_entry_referral( e ) ) {
242 /* parent is a referral, don't allow add */
243 /* parent is an alias, don't allow add */
244 struct berval **refs = get_entry_referrals( be,
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_modify: entry is referral\n",
251 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
252 e->e_dn, NULL, refs, NULL );
254 ber_bvecfree( refs );
258 /* Modify the entry */
259 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e, &text );
261 if( rc != LDAP_SUCCESS ) {
262 Debug( LDAP_DEBUG_TRACE,
263 "bdb_modify: modify failed (%d)\n",
266 case DB_LOCK_DEADLOCK:
267 case DB_LOCK_NOTGRANTED:
273 /* change the entry itself */
274 rc = bdb_id2entry_update( be, ltid, e );
276 Debug( LDAP_DEBUG_TRACE,
277 "bdb_modify: id2entry update failed (%d)\n",
280 case DB_LOCK_DEADLOCK:
281 case DB_LOCK_NOTGRANTED:
284 text = "entry update failed";
288 rc = txn_commit( ltid, 0 );
290 op->o_private = NULL;
293 Debug( LDAP_DEBUG_TRACE,
294 "bdb_modify: txn_commit failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
297 text = "commit failed";
299 Debug( LDAP_DEBUG_TRACE,
300 "bdb_modify: updated id=%08x dn=\"%s\"\n",
301 e->e_id, e->e_dn, 0 );
307 send_ldap_result( conn, op, rc,
308 NULL, text, NULL, NULL );
310 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
311 ldap_pvt_thread_yield();
312 txn_checkpoint( bdb->bi_dbenv,
313 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
319 op->o_private = NULL;
323 bdb_entry_return( be, e );
338 /* char *desc = mod->sm_desc->ad_cname->bv_val; */
339 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
341 a = attr_find( e->e_attrs, mod->sm_desc );
343 /* check if the values we're adding already exist */
345 if( mr == NULL || !mr->smr_match ) {
346 /* do not allow add of additional attribute
347 if no equality rule exists */
348 return LDAP_INAPPROPRIATE_MATCHING;
351 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
354 const char *text = NULL;
355 struct berval *asserted;
357 rc = value_normalize( mod->sm_desc,
363 if( rc != LDAP_SUCCESS ) return rc;
365 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
367 int rc = value_match( &match, mod->sm_desc, mr,
368 SLAP_MR_MODIFY_MATCHING,
369 a->a_vals[j], asserted, &text );
371 if( rc == LDAP_SUCCESS && match == 0 ) {
372 ber_bvfree( asserted );
373 return LDAP_TYPE_OR_VALUE_EXISTS;
377 ber_bvfree( asserted );
382 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
383 /* this should return result return of attr_merge */
399 char *desc = mod->sm_desc->ad_cname->bv_val;
400 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
402 /* delete the entire attribute */
403 if ( mod->sm_bvalues == NULL ) {
404 Debug( LDAP_DEBUG_TRACE,
405 "bdb_modify_delete: removing entire attribute %s\n",
407 return attr_delete( &e->e_attrs, mod->sm_desc )
408 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
411 if( mr == NULL || !mr->smr_match ) {
412 /* disallow specific attributes from being deleted if
414 return LDAP_INAPPROPRIATE_MATCHING;
417 /* delete specific values - find the attribute first */
418 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
419 Debug( LDAP_DEBUG_TRACE,
420 "bdb_modify_delete: could not find attribute %s\n",
422 return LDAP_NO_SUCH_ATTRIBUTE;
425 /* find each value to delete */
426 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
428 const char *text = NULL;
430 struct berval *asserted;
432 rc = value_normalize( mod->sm_desc,
438 if( rc != LDAP_SUCCESS ) return rc;
441 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
443 int rc = value_match( &match, mod->sm_desc, mr,
444 SLAP_MR_MODIFY_MATCHING,
445 a->a_vals[j], asserted, &text );
447 if( rc == LDAP_SUCCESS && match != 0 ) {
451 /* found a matching value */
455 ber_bvfree( a->a_vals[j] );
456 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
457 a->a_vals[k - 1] = a->a_vals[k];
459 a->a_vals[k - 1] = NULL;
464 ber_bvfree( asserted );
466 /* looked through them all w/o finding it */
468 Debug( LDAP_DEBUG_TRACE,
469 "bdb_modify_delete: could not find value for attr %s\n",
471 return LDAP_NO_SUCH_ATTRIBUTE;
475 /* if no values remain, delete the entire attribute */
476 if ( a->a_vals[0] == NULL ) {
477 Debug( LDAP_DEBUG_TRACE,
478 "bdb_modify_delete: removing entire attribute %s\n",
480 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
481 return LDAP_NO_SUCH_ATTRIBUTE;
495 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
497 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
501 if ( mod->sm_bvalues != NULL &&
502 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )