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";
215 ldap_pvt_thread_yield();
219 /* begin transaction */
220 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
221 bdb->bi_db_opflags );
224 Debug( LDAP_DEBUG_TRACE,
225 "bdb_modify: txn_begin failed: %s (%d)\n",
226 db_strerror(rc), rc, 0 );
228 text = "internal error";
234 opinfo.boi_txn = ltid;
236 op->o_private = &opinfo;
239 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
242 Debug( LDAP_DEBUG_TRACE,
243 "bdb_modify: dn2entry failed (%d)\n",
246 case DB_LOCK_DEADLOCK:
247 case DB_LOCK_NOTGRANTED:
254 text = "internal error";
258 /* acquire and lock entry */
260 char* matched_dn = NULL;
263 if ( matched != NULL ) {
264 matched_dn = ch_strdup( matched->e_dn );
265 refs = is_entry_referral( matched )
266 ? get_entry_referrals( be, conn, op, matched )
268 bdb_entry_return( be, matched );
272 refs = referral_rewrite( default_referral,
273 NULL, dn, LDAP_SCOPE_DEFAULT );
276 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
277 matched_dn, NULL, refs, NULL );
279 ber_bvarray_free( refs );
285 if ( !manageDSAit && is_entry_referral( e ) ) {
286 /* parent is a referral, don't allow add */
287 /* parent is an alias, don't allow add */
288 BerVarray refs = get_entry_referrals( be,
291 Debug( LDAP_DEBUG_TRACE,
292 "bdb_modify: entry is referral\n",
295 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
296 e->e_dn, NULL, refs, NULL );
298 ber_bvarray_free( refs );
302 /* Modify the entry */
303 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
304 &text, textbuf, textlen );
306 if( rc != LDAP_SUCCESS ) {
307 Debug( LDAP_DEBUG_TRACE,
308 "bdb_modify: modify failed (%d)\n",
311 case DB_LOCK_DEADLOCK:
312 case DB_LOCK_NOTGRANTED:
318 /* change the entry itself */
319 rc = bdb_id2entry_update( be, ltid, e );
321 Debug( LDAP_DEBUG_TRACE,
322 "bdb_modify: id2entry update failed (%d)\n",
325 case DB_LOCK_DEADLOCK:
326 case DB_LOCK_NOTGRANTED:
329 text = "entry update failed";
334 rc = txn_commit( ltid, 0 );
337 op->o_private = NULL;
340 Debug( LDAP_DEBUG_TRACE,
341 "bdb_modify: txn_commit failed: %s (%d)\n",
342 db_strerror(rc), rc, 0 );
344 text = "commit failed";
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_modify: updated id=%08lx dn=\"%s\"\n",
349 e->e_id, e->e_dn, 0 );
355 send_ldap_result( conn, op, rc,
356 NULL, text, NULL, NULL );
358 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
359 ldap_pvt_thread_yield();
360 TXN_CHECKPOINT( bdb->bi_dbenv,
361 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
367 op->o_private = NULL;
371 bdb_entry_return( be, e );
386 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
387 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
389 a = attr_find( e->e_attrs, mod->sm_desc );
391 /* check if the values we're adding already exist */
393 if( mr == NULL || !mr->smr_match ) {
394 /* do not allow add of additional attribute
395 if no equality rule exists */
396 return LDAP_INAPPROPRIATE_MATCHING;
399 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
402 const char *text = NULL;
403 struct berval asserted;
405 rc = value_normalize( mod->sm_desc,
411 if( rc != LDAP_SUCCESS ) return rc;
413 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
415 int rc = value_match( &match, mod->sm_desc, mr,
416 SLAP_MR_VALUE_SYNTAX_MATCH,
417 &a->a_vals[j], &asserted, &text );
419 if( rc == LDAP_SUCCESS && match == 0 ) {
420 free( asserted.bv_val );
421 return LDAP_TYPE_OR_VALUE_EXISTS;
425 free( asserted.bv_val );
430 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
431 /* this should return result of attr_merge */
447 char *desc = mod->sm_desc->ad_cname.bv_val;
448 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
450 /* delete the entire attribute */
451 if ( mod->sm_bvalues == NULL ) {
452 Debug( LDAP_DEBUG_TRACE,
453 "bdb_modify_delete: removing entire attribute %s\n",
455 return attr_delete( &e->e_attrs, mod->sm_desc )
456 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
459 if( mr == NULL || !mr->smr_match ) {
460 /* disallow specific attributes from being deleted if
462 return LDAP_INAPPROPRIATE_MATCHING;
465 /* delete specific values - find the attribute first */
466 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
467 Debug( LDAP_DEBUG_TRACE,
468 "bdb_modify_delete: could not find attribute %s\n",
470 return LDAP_NO_SUCH_ATTRIBUTE;
473 /* find each value to delete */
474 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
476 const char *text = NULL;
478 struct berval asserted;
480 rc = value_normalize( mod->sm_desc,
486 if( rc != LDAP_SUCCESS ) return rc;
489 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
491 int rc = value_match( &match, mod->sm_desc, mr,
492 SLAP_MR_VALUE_SYNTAX_MATCH,
493 &a->a_vals[j], &asserted, &text );
495 if( rc == LDAP_SUCCESS && match != 0 ) {
499 /* found a matching value */
503 free( a->a_vals[j].bv_val );
504 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
505 a->a_vals[k - 1] = a->a_vals[k];
507 a->a_vals[k - 1].bv_val = NULL;
508 a->a_vals[k - 1].bv_len = 0;
513 free( asserted.bv_val );
515 /* looked through them all w/o finding it */
517 Debug( LDAP_DEBUG_TRACE,
518 "bdb_modify_delete: could not find value for attr %s\n",
520 return LDAP_NO_SUCH_ATTRIBUTE;
524 /* if no values remain, delete the entire attribute */
525 if ( a->a_vals[0].bv_val == NULL ) {
526 Debug( LDAP_DEBUG_TRACE,
527 "bdb_modify_delete: removing entire attribute %s\n",
529 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
530 return LDAP_NO_SUCH_ATTRIBUTE;
544 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
546 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
550 if ( mod->sm_bvalues != NULL &&
551 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )