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(
27 Modifications *modlist,
34 Attribute *save_attrs;
36 Debug(LDAP_DEBUG_TRACE, "bdb_modify_internal: %s\n", dn, 0, 0);
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 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
56 *text = "modify: add values failed";
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 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
67 *text = "modify: delete values failed";
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 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
78 *text = "modify: replace values failed";
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 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
97 *text = "modify: (soft)add values failed";
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
152 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
156 int manageDSAit = get_manageDSAit( op );
157 const char *text = NULL;
159 struct bdb_op_info opinfo;
161 Debug(LDAP_DEBUG_ARGS, "bdb_back_modify: %s\n", dn, 0, 0);
164 /* transaction retry */
165 retry: rc = txn_abort( ltid );
167 op->o_private = NULL;
170 text = "internal error";
175 /* begin transaction */
176 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
178 Debug( LDAP_DEBUG_TRACE,
179 "bdb_modify: txn_begin failed: %s (%d)\n",
180 db_strerror(rc), rc, 0 );
182 text = "internal error";
187 opinfo.boi_txn = ltid;
189 op->o_private = &opinfo;
192 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
195 Debug( LDAP_DEBUG_TRACE,
196 "bdb_modify: dn2entry failed (%d)\n",
199 case DB_LOCK_DEADLOCK:
200 case DB_LOCK_NOTGRANTED:
207 text = "internal error";
211 /* acquire and lock entry */
213 char* matched_dn = NULL;
214 struct berval **refs = NULL;
216 if ( matched != NULL ) {
217 matched_dn = ch_strdup( matched->e_dn );
218 refs = is_entry_referral( matched )
219 ? get_entry_referrals( be, conn, op, matched )
221 bdb_entry_return( be, matched );
224 refs = default_referral;
227 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
228 matched_dn, NULL, refs, NULL );
230 if ( matched != NULL ) {
231 ber_bvecfree( refs );
238 if ( !manageDSAit && is_entry_referral( e ) ) {
239 /* parent is a referral, don't allow add */
240 /* parent is an alias, don't allow add */
241 struct berval **refs = get_entry_referrals( be,
244 Debug( LDAP_DEBUG_TRACE,
245 "bdb_modify: entry is referral\n",
248 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
249 e->e_dn, NULL, refs, NULL );
251 ber_bvecfree( refs );
255 /* Modify the entry */
256 rc = bdb_modify_internal( be, conn, op, ltid, ndn, modlist, e, &text );
258 if( rc != LDAP_SUCCESS ) {
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_modify: modify failed (%d)\n",
263 case DB_LOCK_DEADLOCK:
264 case DB_LOCK_NOTGRANTED:
270 /* change the entry itself */
271 rc = bdb_id2entry_update( be, ltid, e );
273 Debug( LDAP_DEBUG_TRACE,
274 "bdb_modify: id2entry update failed (%d)\n",
277 case DB_LOCK_DEADLOCK:
278 case DB_LOCK_NOTGRANTED:
281 text = "entry update failed";
285 rc = txn_commit( ltid, 0 );
287 op->o_private = NULL;
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_modify: txn_commit failed: %s (%d)\n",
292 db_strerror(rc), rc, 0 );
294 text = "commit failed";
296 Debug( LDAP_DEBUG_TRACE,
297 "bdb_modify: added id=%08x dn=\"%s\"\n",
298 e->e_id, e->e_dn, 0 );
304 send_ldap_result( conn, op, rc,
305 NULL, text, NULL, NULL );
310 op->o_private = NULL;
313 bdb_entry_return( be, e );
327 /* char *desc = mod->sm_desc->ad_cname->bv_val; */
328 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
330 a = attr_find( e->e_attrs, mod->sm_desc );
332 /* check if the values we're adding already exist */
334 if( mr == NULL || !mr->smr_match ) {
335 /* do not allow add of additional attribute
336 if no equality rule exists */
337 return LDAP_INAPPROPRIATE_MATCHING;
340 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
343 const char *text = NULL;
344 struct berval *asserted;
346 rc = value_normalize( mod->sm_desc,
352 if( rc != LDAP_SUCCESS ) return rc;
354 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
356 int rc = value_match( &match, mod->sm_desc, mr,
357 SLAP_MR_MODIFY_MATCHING,
358 a->a_vals[j], asserted, &text );
360 if( rc == LDAP_SUCCESS && match == 0 ) {
361 ber_bvfree( asserted );
362 return LDAP_TYPE_OR_VALUE_EXISTS;
366 ber_bvfree( asserted );
371 if( attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 ) {
372 /* this should return result return of attr_merge */
388 char *desc = mod->sm_desc->ad_cname->bv_val;
389 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
391 /* delete the entire attribute */
392 if ( mod->sm_bvalues == NULL ) {
393 Debug( LDAP_DEBUG_TRACE,
394 "bdb_modify_delete: removing entire attribute %s\n",
396 return attr_delete( &e->e_attrs, mod->sm_desc )
397 ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS;
400 if( mr == NULL || !mr->smr_match ) {
401 /* disallow specific attributes from being deleted if
403 return LDAP_INAPPROPRIATE_MATCHING;
406 /* delete specific values - find the attribute first */
407 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
408 Debug( LDAP_DEBUG_TRACE,
409 "bdb_modify_delete: could not find attribute %s\n",
411 return LDAP_NO_SUCH_ATTRIBUTE;
414 /* find each value to delete */
415 for ( i = 0; mod->sm_bvalues[i] != NULL; i++ ) {
417 const char *text = NULL;
419 struct berval *asserted;
421 rc = value_normalize( mod->sm_desc,
427 if( rc != LDAP_SUCCESS ) return rc;
430 for ( j = 0; a->a_vals[j] != NULL; j++ ) {
432 int rc = value_match( &match, mod->sm_desc, mr,
433 SLAP_MR_MODIFY_MATCHING,
434 a->a_vals[j], asserted, &text );
436 if( rc == LDAP_SUCCESS && match != 0 ) {
440 /* found a matching value */
444 ber_bvfree( a->a_vals[j] );
445 for ( k = j + 1; a->a_vals[k] != NULL; k++ ) {
446 a->a_vals[k - 1] = a->a_vals[k];
448 a->a_vals[k - 1] = NULL;
453 ber_bvfree( asserted );
455 /* looked through them all w/o finding it */
457 Debug( LDAP_DEBUG_TRACE,
458 "bdb_modify_delete: could not find value for attr %s\n",
460 return LDAP_NO_SUCH_ATTRIBUTE;
464 /* if no values remain, delete the entire attribute */
465 if ( a->a_vals[0] == NULL ) {
466 Debug( LDAP_DEBUG_TRACE,
467 "bdb_modify_delete: removing entire attribute %s\n",
469 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
470 return LDAP_NO_SUCH_ATTRIBUTE;
484 int rc = attr_delete( &e->e_attrs, mod->sm_desc );
486 if( rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_ATTRIBUTE ) {
490 if ( mod->sm_bvalues != NULL &&
491 attr_merge( e, mod->sm_desc, mod->sm_bvalues ) != 0 )