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 /* If objectClass was modified, reset the flags */
121 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
125 /* check if modified attribute was indexed */
126 err = bdb_index_is_indexed( be, mod->sm_desc );
127 if ( err == LDAP_SUCCESS ) {
128 ap = attr_find( save_attrs, mod->sm_desc );
129 if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
131 ap = attr_find( e->e_attrs, mod->sm_desc );
132 if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
136 /* check that the entry still obeys the schema */
137 rc = entry_schema_check( be, e, save_attrs, text, textbuf, textlen );
138 if ( rc != LDAP_SUCCESS ) {
139 attrs_free( e->e_attrs );
140 e->e_attrs = save_attrs;
141 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
146 /* update the indices of the modified attributes */
148 /* start with deleting the old index entries */
149 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
150 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
151 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
152 e->e_id, SLAP_INDEX_DELETE_OP );
153 if ( rc != LDAP_SUCCESS ) {
154 attrs_free( e->e_attrs );
155 e->e_attrs = save_attrs;
156 Debug( LDAP_DEBUG_ANY,
157 "Attribute index delete failure",
161 ap->a_flags &= ~SLAP_ATTR_IXDEL;
165 /* add the new index entries */
166 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
167 if (ap->a_flags & SLAP_ATTR_IXADD) {
168 rc = bdb_index_values( be, tid, ap->a_desc, ap->a_vals,
169 e->e_id, SLAP_INDEX_ADD_OP );
170 if ( rc != LDAP_SUCCESS ) {
171 attrs_free( e->e_attrs );
172 e->e_attrs = save_attrs;
173 Debug( LDAP_DEBUG_ANY,
174 "Attribute index add failure",
178 ap->a_flags &= ~SLAP_ATTR_IXADD;
193 Modifications *modlist )
195 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
199 int manageDSAit = get_manageDSAit( op );
200 const char *text = NULL;
201 char textbuf[SLAP_TEXT_BUFLEN];
202 size_t textlen = sizeof textbuf;
204 struct bdb_op_info opinfo;
206 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn->bv_val, 0, 0 );
209 retry: /* transaction retry */
210 Debug(LDAP_DEBUG_TRACE,
211 "bdb_modify: retrying...\n", 0, 0, 0);
212 rc = txn_abort( ltid );
214 op->o_private = NULL;
217 text = "internal error";
220 ldap_pvt_thread_yield();
224 /* begin transaction */
225 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
226 bdb->bi_db_opflags );
229 Debug( LDAP_DEBUG_TRACE,
230 "bdb_modify: txn_begin failed: %s (%d)\n",
231 db_strerror(rc), rc, 0 );
233 text = "internal error";
239 opinfo.boi_txn = ltid;
241 op->o_private = &opinfo;
244 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_modify: dn2entry failed (%d)\n",
251 case DB_LOCK_DEADLOCK:
252 case DB_LOCK_NOTGRANTED:
259 text = "internal error";
263 /* acquire and lock entry */
265 char* matched_dn = NULL;
268 if ( matched != NULL ) {
269 matched_dn = ch_strdup( matched->e_dn );
270 refs = is_entry_referral( matched )
271 ? get_entry_referrals( be, conn, op, matched )
273 bdb_entry_return( be, matched );
277 refs = referral_rewrite( default_referral,
278 NULL, dn, LDAP_SCOPE_DEFAULT );
281 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
282 matched_dn, NULL, refs, NULL );
284 ber_bvarray_free( refs );
290 if ( !manageDSAit && is_entry_referral( e ) ) {
291 /* parent is a referral, don't allow add */
292 /* parent is an alias, don't allow add */
293 BerVarray refs = get_entry_referrals( be,
296 Debug( LDAP_DEBUG_TRACE,
297 "bdb_modify: entry is referral\n",
300 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
301 e->e_dn, NULL, refs, NULL );
303 ber_bvarray_free( refs );
307 /* Modify the entry */
308 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
309 &text, textbuf, textlen );
311 if( rc != LDAP_SUCCESS ) {
312 Debug( LDAP_DEBUG_TRACE,
313 "bdb_modify: modify failed (%d)\n",
316 case DB_LOCK_DEADLOCK:
317 case DB_LOCK_NOTGRANTED:
323 /* change the entry itself */
324 rc = bdb_id2entry_update( be, ltid, e );
326 Debug( LDAP_DEBUG_TRACE,
327 "bdb_modify: id2entry update failed (%d)\n",
330 case DB_LOCK_DEADLOCK:
331 case DB_LOCK_NOTGRANTED:
334 text = "entry update failed";
339 rc = txn_commit( ltid, 0 );
342 op->o_private = NULL;
345 Debug( LDAP_DEBUG_TRACE,
346 "bdb_modify: txn_commit failed: %s (%d)\n",
347 db_strerror(rc), rc, 0 );
349 text = "commit failed";
352 Debug( LDAP_DEBUG_TRACE,
353 "bdb_modify: updated id=%08lx dn=\"%s\"\n",
354 e->e_id, e->e_dn, 0 );
360 send_ldap_result( conn, op, rc,
361 NULL, text, NULL, NULL );
363 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
364 ldap_pvt_thread_yield();
365 TXN_CHECKPOINT( bdb->bi_dbenv,
366 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
372 op->o_private = NULL;
376 bdb_entry_return( be, e );
391 /* char *desc = mod->sm_desc->ad_cname.bv_val; */
392 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
394 a = attr_find( e->e_attrs, mod->sm_desc );
396 /* check if the values we're adding already exist */
397 if( mr == NULL || !mr->smr_match ) {
399 /* do not allow add of additional attribute
400 if no equality rule exists */
401 return LDAP_INAPPROPRIATE_MATCHING;
404 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
405 /* test asserted values against existing values */
407 for( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
408 int rc = ber_bvcmp( &mod->sm_bvalues[i],
412 /* value exists already */
413 return LDAP_TYPE_OR_VALUE_EXISTS;
418 /* test asserted values against themselves */
419 for( j = 0; j < i; j++ ) {
420 int rc = ber_bvcmp( &mod->sm_bvalues[i],
421 &mod->sm_bvalues[j] );
424 /* value exists already */
425 return LDAP_TYPE_OR_VALUE_EXISTS;
431 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
433 const char *text = NULL;
434 struct berval asserted;
436 rc = value_normalize( mod->sm_desc,
442 if( rc != LDAP_SUCCESS ) return rc;
445 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
446 int rc = value_match( &match, mod->sm_desc, mr,
447 SLAP_MR_VALUE_SYNTAX_MATCH,
448 &a->a_vals[j], &asserted, &text );
450 if( rc == LDAP_SUCCESS && match == 0 ) {
451 free( asserted.bv_val );
452 return LDAP_TYPE_OR_VALUE_EXISTS;
457 for ( j = 0; j < i; j++ ) {
458 int rc = value_match( &match, mod->sm_desc, mr,
459 SLAP_MR_VALUE_SYNTAX_MATCH,
460 &mod->sm_bvalues[j], &asserted, &text );
462 if( rc == LDAP_SUCCESS && match == 0 ) {
463 free( asserted.bv_val );
464 return LDAP_TYPE_OR_VALUE_EXISTS;
468 free( asserted.bv_val );
473 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
474 /* this should return result of attr_merge */
490 char *desc = mod->sm_desc->ad_cname.bv_val;
491 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
493 /* delete the entire attribute */
494 if ( mod->sm_bvalues == NULL ) {
495 Debug( LDAP_DEBUG_TRACE,
496 "bdb_modify_delete: removing entire attribute %s\n",
498 return attr_delete( &e->e_attrs, mod->sm_desc )
499 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
502 if( mr == NULL || !mr->smr_match ) {
503 /* disallow specific attributes from being deleted if
505 return LDAP_INAPPROPRIATE_MATCHING;
508 /* delete specific values - find the attribute first */
509 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
510 Debug( LDAP_DEBUG_TRACE,
511 "bdb_modify_delete: could not find attribute %s\n",
513 return LDAP_NO_SUCH_ATTRIBUTE;
516 /* find each value to delete */
517 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
519 const char *text = NULL;
521 struct berval asserted;
523 rc = value_normalize( mod->sm_desc,
529 if( rc != LDAP_SUCCESS ) return rc;
532 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
534 int rc = value_match( &match, mod->sm_desc, mr,
535 SLAP_MR_VALUE_SYNTAX_MATCH,
536 &a->a_vals[j], &asserted, &text );
538 if( rc == LDAP_SUCCESS && match != 0 ) {
542 /* found a matching value */
546 free( a->a_vals[j].bv_val );
547 for ( k = j + 1; a->a_vals[k].bv_val != NULL; k++ ) {
548 a->a_vals[k - 1] = a->a_vals[k];
550 a->a_vals[k - 1].bv_val = NULL;
551 a->a_vals[k - 1].bv_len = 0;
556 free( asserted.bv_val );
558 /* looked through them all w/o finding it */
560 Debug( LDAP_DEBUG_TRACE,
561 "bdb_modify_delete: could not find value for attr %s\n",
563 return LDAP_NO_SUCH_ATTRIBUTE;
567 /* if no values remain, delete the entire attribute */
568 if ( a->a_vals[0].bv_val == NULL ) {
569 Debug( LDAP_DEBUG_TRACE,
570 "bdb_modify_delete: removing entire attribute %s\n",
572 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
573 return LDAP_NO_SUCH_ATTRIBUTE;
587 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
589 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
594 if ( mod->sm_bvalues ) {
595 rc = add_values( e, mod, dn );