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,
35 Attribute *save_attrs;
37 Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
40 if ( !acl_check_modlist( be, conn, op, e, modlist )) {
41 return LDAP_INSUFFICIENT_ACCESS;
44 save_attrs = e->e_attrs;
45 e->e_attrs = attrs_dup( e->e_attrs );
47 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
50 switch ( mod->sm_op ) {
52 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: add\n", 0, 0, 0);
53 err = add_values( e, mod, op->o_ndn );
55 if( err != LDAP_SUCCESS ) {
56 *text = "modify: add values failed";
57 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
63 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: delete\n", 0, 0, 0);
64 err = delete_values( e, mod, op->o_ndn );
65 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
66 if( err != LDAP_SUCCESS ) {
67 *text = "modify: delete values failed";
68 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
73 case LDAP_MOD_REPLACE:
74 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: replace\n", 0, 0, 0);
75 err = replace_values( e, mod, op->o_ndn );
76 assert( err != LDAP_TYPE_OR_VALUE_EXISTS );
77 if( err != LDAP_SUCCESS ) {
78 *text = "modify: replace values failed";
79 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
84 case SLAP_MOD_SOFTADD:
85 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: softadd\n", 0, 0, 0);
86 /* Avoid problems in index_add_mods()
87 * We need to add index if necessary.
89 mod->sm_op = LDAP_MOD_ADD;
90 err = add_values( e, mod, op->o_ndn );
92 if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
96 if( err != LDAP_SUCCESS ) {
97 *text = "modify: (soft)add values failed";
98 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
104 Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
106 *text = "Invalid modify operation";
108 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
112 if ( err != LDAP_SUCCESS ) {
113 attrs_free( e->e_attrs );
114 e->e_attrs = save_attrs;
115 /* unlock entry, delete from cache */
120 /* check that the entry still obeys the schema */
121 rc = entry_schema_check( e, save_attrs, text, textbuf, textlen );
122 if ( rc != LDAP_SUCCESS ) {
123 attrs_free( e->e_attrs );
124 e->e_attrs = save_attrs;
125 Debug( LDAP_DEBUG_ANY, "entry failed schema check: %s\n",
131 /* delete indices for old attributes */
132 rc = index_entry_del( be, tid, e, save_attrs);
134 /* add indices for new attributes */
135 rc = index_entry_add( be, tid, e, e->e_attrs);
138 attrs_free( save_attrs );
151 Modifications *modlist )
153 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
157 int manageDSAit = get_manageDSAit( op );
158 const char *text = NULL;
159 char textbuf[SLAP_TEXT_BUFLEN];
160 size_t textlen = sizeof textbuf;
162 struct bdb_op_info opinfo;
164 Debug( LDAP_DEBUG_ARGS, "bdb_modify: %s\n", dn, 0, 0 );
167 retry: /* transaction retry */
168 Debug(LDAP_DEBUG_TRACE,
169 "bdb_modify: retrying...\n", 0, 0, 0);
170 rc = txn_abort( ltid );
172 op->o_private = NULL;
175 text = "internal error";
180 /* begin transaction */
181 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
184 Debug( LDAP_DEBUG_TRACE,
185 "bdb_modify: txn_begin failed: %s (%d)\n",
186 db_strerror(rc), rc, 0 );
188 text = "internal error";
193 opinfo.boi_txn = ltid;
195 op->o_private = &opinfo;
198 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
201 Debug( LDAP_DEBUG_TRACE,
202 "bdb_modify: dn2entry failed (%d)\n",
205 case DB_LOCK_DEADLOCK:
206 case DB_LOCK_NOTGRANTED:
213 text = "internal error";
217 /* acquire and lock entry */
219 char* matched_dn = NULL;
220 struct berval **refs = NULL;
222 if ( matched != NULL ) {
223 matched_dn = ch_strdup( matched->e_dn );
224 refs = is_entry_referral( matched )
225 ? get_entry_referrals( be, conn, op, matched )
227 bdb_entry_return( be, matched );
231 refs = default_referral;
234 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
235 matched_dn, NULL, refs, NULL );
237 if ( matched != NULL ) {
238 ber_bvecfree( refs );
245 if ( !manageDSAit && is_entry_referral( e ) ) {
246 /* parent is a referral, don't allow add */
247 /* parent is an alias, don't allow add */
248 struct berval **refs = get_entry_referrals( be,
251 Debug( LDAP_DEBUG_TRACE,
252 "bdb_modify: entry is referral\n",
255 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
256 e->e_dn, NULL, refs, NULL );
258 ber_bvecfree( refs );
262 /* Modify the entry */
263 rc = bdb_modify_internal( be, conn, op, ltid, modlist, e,
264 &text, textbuf, textlen );
266 if( rc != LDAP_SUCCESS ) {
267 Debug( LDAP_DEBUG_TRACE,
268 "bdb_modify: modify failed (%d)\n",
271 case DB_LOCK_DEADLOCK:
272 case DB_LOCK_NOTGRANTED:
278 /* change the entry itself */
279 rc = bdb_id2entry_update( be, ltid, e );
281 Debug( LDAP_DEBUG_TRACE,
282 "bdb_modify: id2entry update failed (%d)\n",
285 case DB_LOCK_DEADLOCK:
286 case DB_LOCK_NOTGRANTED:
289 text = "entry update failed";
293 rc = txn_commit( ltid, 0 );
295 op->o_private = NULL;
298 Debug( LDAP_DEBUG_TRACE,
299 "bdb_modify: txn_commit failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
302 text = "commit failed";
304 Debug( LDAP_DEBUG_TRACE,
305 "bdb_modify: updated id=%08x dn=\"%s\"\n",
306 e->e_id, e->e_dn, 0 );
312 send_ldap_result( conn, op, rc,
313 NULL, text, NULL, NULL );
315 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
316 ldap_pvt_thread_yield();
317 txn_checkpoint( bdb->bi_dbenv,
318 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
324 op->o_private = NULL;
328 bdb_entry_return( be, e );
343 /* char *desc = mod->sm_desc->ad_cname->bv_val; */
344 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
346 a = attr_find( e->e_attrs, mod->sm_desc );
348 /* check if the values we're adding already exist */
350 if( mr == NULL || !mr->smr_match ) {
351 /* do not allow add of additional attribute
352 if no equality rule exists */
353 return LDAP_INAPPROPRIATE_MATCHING;
356 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
359 const char *text = NULL;
360 struct berval *asserted;
362 rc = value_normalize( mod->sm_desc,
368 if( rc != LDAP_SUCCESS ) return rc;
370 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
372 int rc = value_match( &match, mod->sm_desc, mr,
373 SLAP_MR_MODIFY_MATCHING,
374 a->a_vals[j], asserted, &text );
376 if( rc == LDAP_SUCCESS && match == 0 ) {
377 ber_bvfree( asserted );
378 return LDAP_TYPE_OR_VALUE_EXISTS;
382 ber_bvfree( asserted );
387 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
388 /* this should return result return of attr_merge */
404 char *desc = mod->sm_desc->ad_cname->bv_val;
405 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
407 /* delete the entire attribute */
408 if ( mod->sm_bvalues == NULL ) {
409 Debug( LDAP_DEBUG_TRACE,
410 "bdb_modify_delete: removing entire attribute %s\n",
412 return attr_delete( &e->e_attrs, mod->sm_desc )
413 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
416 if( mr == NULL || !mr->smr_match ) {
417 /* disallow specific attributes from being deleted if
419 return LDAP_INAPPROPRIATE_MATCHING;
422 /* delete specific values - find the attribute first */
423 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
424 Debug( LDAP_DEBUG_TRACE,
425 "bdb_modify_delete: could not find attribute %s\n",
427 return LDAP_NO_SUCH_ATTRIBUTE;
430 /* find each value to delete */
431 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
433 const char *text = NULL;
435 struct berval *asserted;
437 rc = value_normalize( mod->sm_desc,
443 if( rc != LDAP_SUCCESS ) return rc;
446 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
448 int rc = value_match( &match, mod->sm_desc, mr,
449 SLAP_MR_MODIFY_MATCHING,
450 a->a_vals[j], asserted, &text );
452 if( rc == LDAP_SUCCESS && match != 0 ) {
456 /* found a matching value */
460 ber_bvfree( a->a_vals[j] );
461 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
462 a->a_vals[k - 1] = a->a_vals[k];
464 a->a_vals[k - 1] = NULL;
469 ber_bvfree( asserted );
471 /* looked through them all w/o finding it */
473 Debug( LDAP_DEBUG_TRACE,
474 "bdb_modify_delete: could not find value for attr %s\n",
476 return LDAP_NO_SUCH_ATTRIBUTE;
480 /* if no values remain, delete the entire attribute */
481 if ( a->a_vals[0] == NULL ) {
482 Debug( LDAP_DEBUG_TRACE,
483 "bdb_modify_delete: removing entire attribute %s\n",
485 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
486 return LDAP_NO_SUCH_ATTRIBUTE;
500 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
502 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
506 if ( mod->sm_bvalues != NULL &&
507 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )