1 /* modify.c - bdb backend modify routine */
4 * Copyright 1998-2002 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,
35 Attribute *save_attrs;
38 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
41 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
42 return LDAP_INSUFFICIENT_ACCESS;
45 save_attrs = e->e_attrs;
46 e->e_attrs = attrs_dup( e->e_attrs );
48 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
51 switch ( mod->sm_op ) {
53 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
54 err = add_values( e, mod, op->o_ndn.bv_val );
56 if( err != LDAP_SUCCESS ) {
57 *text = "modify: add values failed";
58 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
64 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
65 err = delete_values( e, mod, op->o_ndn.bv_val );
66 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
67 if( err != LDAP_SUCCESS ) {
68 *text = "modify: delete values failed";
69 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
74 case LDAP_MOD_REPLACE:
75 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
76 err = replace_values( e, mod, op->o_ndn.bv_val );
77 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
78 if( err != LDAP_SUCCESS ) {
79 *text = "modify: replace values failed";
80 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
85 case SLAP_MOD_SOFTADD:
86 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
87 /* Avoid problems in index_add_mods()
88 * We need to add index if necessary.
90 mod->sm_op = LDAP_MOD_ADD;
91 err = add_values( e, mod, op->o_ndn.bv_val );
93 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
97 if( err != LDAP_SUCCESS ) {
98 *text = "modify: (soft)add values failed";
99 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
105 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
107 *text = "Invalid modify operation";
109 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
113 if ( err != LDAP_SUCCESS ) {
114 attrs_free( e->e_attrs );
115 e->e_attrs = save_attrs;
116 /* unlock entry, delete from cache */
120 /* check if modified attribute was indexed */
121 err = bdb_index_is_indexed( be, mod->sm_desc );
122 if ( err == LDAP_SUCCESS ) {
123 ap = attr_find( save_attrs, mod->sm_desc );
124 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
126 ap = attr_find( e->e_attrs, mod->sm_desc );
127 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
131 /* check that the entry still obeys the schema */
132 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
133 if ( rc != LDAP_SUCCESS ) {
134 attrs_free( e->e_attrs );
135 e->e_attrs = save_attrs;
136 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
141 /* update the indices of the modified attributes */
143 /* start with deleting the old index entries */
144 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
145 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
146 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
147 e->e_id, SLAP_INDEX_DELETE_OP );
148 if ( rc != LDAP_SUCCESS ) {
149 attrs_free( e->e_attrs );
150 e->e_attrs = save_attrs;
151 Debug( LDAP_DEBUG_ANY,
152 "Attribute index delete failure",
156 ap->a_flags &= ~SLAP_ATTR_IXDEL;
160 /* add the new index entries */
161 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
162 if (ap->a_flags & SLAP_ATTR_IXADD) {
163 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
164 e->e_id, SLAP_INDEX_ADD_OP );
165 if ( rc != LDAP_SUCCESS ) {
166 attrs_free( e->e_attrs );
167 e->e_attrs = save_attrs;
168 Debug( LDAP_DEBUG_ANY,
169 "Attribute index add failure",
173 ap->a_flags &= ~SLAP_ATTR_IXADD;
188 Modifications *modlist )
190 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
194 int manageDSAit = get_manageDSAit( op );
195 const char *text = NULL;
196 char textbuf[SLAP_TEXT_BUFLEN];
197 size_t textlen = sizeof textbuf;
199 struct bdb_op_info opinfo;
201 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
204 retry: /* transaction retry */
205 Debug(LDAP_DEBUG_TRACE,
206 "bdb_modify: retrying...\n", 0, 0, 0);
207 rc = txn_abort( ltid );
209 op->o_private = NULL;
212 text = "internal error";
218 /* begin transaction */
219 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
220 bdb->bi_db_opflags );
223 Debug( LDAP_DEBUG_TRACE,
224 "bdb_modify: txn_begin failed: %s (%d)\n",
225 db_strerror(rc), rc, 0 );
227 text = "internal error";
233 opinfo.boi_txn = ltid;
235 op->o_private = &opinfo;
238 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
241 Debug( LDAP_DEBUG_TRACE,
242 "bdb_modify: dn2entry failed (%d)\n",
245 case DB_LOCK_DEADLOCK:
246 case DB_LOCK_NOTGRANTED:
253 text = "internal error";
257 /* acquire and lock entry */
259 char* matched_dn = NULL;
262 if ( matched != NULL ) {
263 matched_dn = ch_strdup( matched->e_dn );
264 refs = is_entry_referral( matched )
265 ? get_entry_referrals( be, conn, op, matched )
267 bdb_entry_return( be, matched );
271 refs = referral_rewrite( default_referral,
272 NULL, dn, LDAP_SCOPE_DEFAULT );
275 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
276 matched_dn, NULL, refs, NULL );
278 ber_bvarray_free( refs );
284 if ( !manageDSAit && is_entry_referral( e ) ) {
285 /* parent is a referral, don't allow add */
286 /* parent is an alias, don't allow add */
287 BerVarray refs = get_entry_referrals( be,
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_modify: entry is referral\n",
294 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
295 e->e_dn, NULL, refs, NULL );
297 ber_bvarray_free( refs );
301 /* Modify the entry */
302 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
303 &text, textbuf, textlen );
305 if( rc != LDAP_SUCCESS ) {
306 Debug( LDAP_DEBUG_TRACE,
307 "bdb_modify: modify failed (%d)\n",
310 case DB_LOCK_DEADLOCK:
311 case DB_LOCK_NOTGRANTED:
317 /* change the entry itself */
318 rc = bdb_id2entry_update( be, ltid, e );
320 Debug( LDAP_DEBUG_TRACE,
321 "bdb_modify: id2entry update failed (%d)\n",
324 case DB_LOCK_DEADLOCK:
325 case DB_LOCK_NOTGRANTED:
328 text = "entry update failed";
333 rc = txn_commit( ltid, 0 );
336 op->o_private = NULL;
339 Debug( LDAP_DEBUG_TRACE,
340 "bdb_modify: txn_commit failed: %s (%d)\n",
341 db_strerror(rc), rc, 0 );
343 text = "commit failed";
346 Debug( LDAP_DEBUG_TRACE,
347 "bdb_modify: updated id=%08lx dn=\"%s\"\n",
348 e->e_id, e->e_dn, 0 );
354 send_ldap_result( conn, op, rc,
355 NULL, text, NULL, NULL );
357 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
358 ldap_pvt_thread_yield();
359 TXN_CHECKPOINT( bdb->bi_dbenv,
360 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
366 op->o_private = NULL;
370 bdb_entry_return( be, e );
385 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
386 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
388 a = attr_find( e->e_attrs, mod->sm_desc );
390 /* check if the values we're adding already exist */
392 if( mr == NULL || !mr->smr_match ) {
393 /* do not allow add of additional attribute
394 if no equality rule exists */
395 return LDAP_INAPPROPRIATE_MATCHING;
398 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
401 const char *text = NULL;
402 struct berval asserted;
404 rc = value_normalize( mod->sm_desc,
410 if( rc != LDAP_SUCCESS ) return rc;
412 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
414 int rc = value_match( &match, mod->sm_desc, mr,
415 SLAP_MR_VALUE_SYNTAX_MATCH,
416 &a->a_vals[j], &asserted, &text );
418 if( rc == LDAP_SUCCESS && match == 0 ) {
419 free( asserted.bv_val );
420 return LDAP_TYPE_OR_VALUE_EXISTS;
424 free( asserted.bv_val );
429 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
430 /* this should return result of attr_merge */
446 char *desc = mod->sm_desc->ad_cname.bv_val;
447 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
449 /* delete the entire attribute */
450 if ( mod->sm_bvalues == NULL ) {
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_modify_delete: removing entire attribute %s\n",
454 return attr_delete( &e->e_attrs, mod->sm_desc )
455 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
458 if( mr == NULL || !mr->smr_match ) {
459 /* disallow specific attributes from being deleted if
461 return LDAP_INAPPROPRIATE_MATCHING;
464 /* delete specific values - find the attribute first */
465 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
466 Debug( LDAP_DEBUG_TRACE,
467 "bdb_modify_delete: could not find attribute %s\n",
469 return LDAP_NO_SUCH_ATTRIBUTE;
472 /* find each value to delete */
473 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
475 const char *text = NULL;
477 struct berval asserted;
479 rc = value_normalize( mod->sm_desc,
485 if( rc != LDAP_SUCCESS ) return rc;
488 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
490 int rc = value_match( &match, mod->sm_desc, mr,
491 SLAP_MR_VALUE_SYNTAX_MATCH,
492 &a->a_vals[j], &asserted, &text );
494 if( rc == LDAP_SUCCESS && match != 0 ) {
498 /* found a matching value */
502 free( a->a_vals[j].bv_val );
503 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
504 a->a_vals[k - 1] = a->a_vals[k];
506 a->a_vals[k - 1].bv_val = NULL;
507 a->a_vals[k - 1].bv_len = 0;
512 free( asserted.bv_val );
514 /* looked through them all w/o finding it */
516 Debug( LDAP_DEBUG_TRACE,
517 "bdb_modify_delete: could not find value for attr %s\n",
519 return LDAP_NO_SUCH_ATTRIBUTE;
523 /* if no values remain, delete the entire attribute */
524 if ( a->a_vals[0].bv_val == NULL ) {
525 Debug( LDAP_DEBUG_TRACE,
526 "bdb_modify_delete: removing entire attribute %s\n",
528 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
529 return LDAP_NO_SUCH_ATTRIBUTE;
543 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
545 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
549 if ( mod->sm_bvalues != NULL &&
550 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )