1 /* modify.cpp - ndb backend modify routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2010 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Howard Chu for inclusion
18 * in OpenLDAP Software. This work was sponsored by MySQL.
24 #include <ac/string.h>
29 /* This is a copy from slapd/mods.c, but with compaction tweaked
30 * to swap values from the tail into deleted slots, to reduce the
31 * overall update traffic.
39 char *textbuf, size_t textlen,
43 MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
46 int i, j, rc = 0, num;
50 /* For ordered vals, we have no choice but to preserve order */
51 if ( mod->sm_desc->ad_type->sat_flags & SLAP_AT_ORDERED_VAL )
52 return modify_delete_vindex( e, mod, permissive, text,
53 textbuf, textlen, idx );
56 * If permissive is set, then the non-existence of an
57 * attribute is not treated as an error.
60 /* delete the entire attribute */
61 if ( mod->sm_values == NULL ) {
62 rc = attr_delete( &e->e_attrs, mod->sm_desc );
66 } else if( rc != LDAP_SUCCESS ) {
68 snprintf( textbuf, textlen,
69 "modify/delete: %s: no such attribute",
70 mod->sm_desc->ad_cname.bv_val );
71 rc = LDAP_NO_SUCH_ATTRIBUTE;
76 /* FIXME: Catch old code that doesn't set sm_numvals.
78 if ( !BER_BVISNULL( &mod->sm_values[mod->sm_numvals] )) {
79 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ );
80 assert( mod->sm_numvals == i );
83 id2 = (int *)ch_malloc( mod->sm_numvals * sizeof( int ));
87 if( mr == NULL || !mr->smr_match ) {
88 /* disallow specific attributes from being deleted if
91 snprintf( textbuf, textlen,
92 "modify/delete: %s: no equality matching rule",
93 mod->sm_desc->ad_cname.bv_val );
94 rc = LDAP_INAPPROPRIATE_MATCHING;
98 /* delete specific values - find the attribute first */
99 if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
105 snprintf( textbuf, textlen,
106 "modify/delete: %s: no such attribute",
107 mod->sm_desc->ad_cname.bv_val );
108 rc = LDAP_NO_SUCH_ATTRIBUTE;
112 if ( mod->sm_nvalues ) {
113 flags = SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
114 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
115 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH;
116 cvals = mod->sm_nvalues;
118 flags = SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX;
119 cvals = mod->sm_values;
122 /* Locate values to delete */
123 for ( i = 0; !BER_BVISNULL( &mod->sm_values[i] ); i++ ) {
125 rc = attr_valfind( a, flags, &cvals[i], &sort, NULL );
126 if ( rc == LDAP_SUCCESS ) {
128 } else if ( rc == LDAP_NO_SUCH_ATTRIBUTE ) {
134 snprintf( textbuf, textlen,
135 "modify/delete: %s: no such value",
136 mod->sm_desc->ad_cname.bv_val );
140 snprintf( textbuf, textlen,
141 "modify/delete: %s: matching rule failed",
142 mod->sm_desc->ad_cname.bv_val );
149 /* Delete the values */
150 for ( i = 0; i < mod->sm_numvals; i++ ) {
151 /* Skip permissive values that weren't found */
154 /* Skip duplicate delete specs */
155 if ( a->a_vals[idx[i]].bv_val == &dummy )
157 /* delete value and mark it as gone */
158 free( a->a_vals[idx[i]].bv_val );
159 a->a_vals[idx[i]].bv_val = &dummy;
160 if( a->a_nvals != a->a_vals ) {
161 free( a->a_nvals[idx[i]].bv_val );
162 a->a_nvals[idx[i]].bv_val = &dummy;
168 for ( i=0; i<num; i++ ) {
169 if ( a->a_vals[i].bv_val != &dummy )
171 for ( --num; num > i && a->a_vals[num].bv_val == &dummy; num-- )
173 a->a_vals[i] = a->a_vals[num];
174 if ( a->a_nvals != a->a_vals )
175 a->a_nvals[i] = a->a_nvals[num];
178 BER_BVZERO( &a->a_vals[num] );
179 if (a->a_nvals != a->a_vals) {
180 BER_BVZERO( &a->a_nvals[num] );
183 /* if no values remain, delete the entire attribute */
184 if ( !a->a_numvals ) {
185 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
186 /* Can never happen */
188 snprintf( textbuf, textlen,
189 "modify/delete: %s: no such attribute",
190 mod->sm_desc->ad_cname.bv_val );
191 rc = LDAP_NO_SUCH_ATTRIBUTE;
200 int ndb_modify_internal(
207 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
210 Modifications *modlist = op->orm_modlist;
211 NdbAttrInfo **modai, *atmp;
212 const NdbDictionary::Dictionary *myDict;
213 const NdbDictionary::Table *myTable;
214 int got_oc = 0, nmods = 0, nai = 0, i, j;
216 Attribute *old = NULL;
218 Debug( LDAP_DEBUG_TRACE, "ndb_modify_internal: 0x%08lx: %s\n",
219 NA->e->e_id, NA->e->e_dn, 0);
221 if ( !acl_check_modlist( op, NA->e, modlist )) {
222 return LDAP_INSUFFICIENT_ACCESS;
225 old = attrs_dup( NA->e->e_attrs );
227 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
231 switch ( mod->sm_op ) {
233 Debug(LDAP_DEBUG_ARGS,
234 "ndb_modify_internal: add %s\n",
235 mod->sm_desc->ad_cname.bv_val, 0, 0);
236 rc = modify_add_values( NA->e, mod, get_permissiveModify(op),
237 text, textbuf, textlen );
238 if( rc != LDAP_SUCCESS ) {
239 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
244 case LDAP_MOD_DELETE:
245 Debug(LDAP_DEBUG_ARGS,
246 "ndb_modify_internal: delete %s\n",
247 mod->sm_desc->ad_cname.bv_val, 0, 0);
248 rc = ndb_modify_delete( NA->e, mod, get_permissiveModify(op),
249 text, textbuf, textlen, NULL );
250 assert( rc != LDAP_TYPE_OR_VALUE_EXISTS );
251 if( rc != LDAP_SUCCESS ) {
252 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
257 case LDAP_MOD_REPLACE:
258 Debug(LDAP_DEBUG_ARGS,
259 "ndb_modify_internal: replace %s\n",
260 mod->sm_desc->ad_cname.bv_val, 0, 0);
261 rc = modify_replace_values( NA->e, mod, get_permissiveModify(op),
262 text, textbuf, textlen );
263 if( rc != LDAP_SUCCESS ) {
264 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
269 case LDAP_MOD_INCREMENT:
270 Debug(LDAP_DEBUG_ARGS,
271 "ndb_modify_internal: increment %s\n",
272 mod->sm_desc->ad_cname.bv_val, 0, 0);
273 rc = modify_increment_values( NA->e, mod, get_permissiveModify(op),
274 text, textbuf, textlen );
275 if( rc != LDAP_SUCCESS ) {
276 Debug(LDAP_DEBUG_ARGS,
277 "ndb_modify_internal: %d %s\n",
282 case SLAP_MOD_SOFTADD:
283 Debug(LDAP_DEBUG_ARGS,
284 "ndb_modify_internal: softadd %s\n",
285 mod->sm_desc->ad_cname.bv_val, 0, 0);
286 mod->sm_op = LDAP_MOD_ADD;
288 rc = modify_add_values( NA->e, mod, get_permissiveModify(op),
289 text, textbuf, textlen );
291 mod->sm_op = SLAP_MOD_SOFTADD;
293 if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
297 if( rc != LDAP_SUCCESS ) {
298 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
303 case SLAP_MOD_SOFTDEL:
304 Debug(LDAP_DEBUG_ARGS,
305 "ndb_modify_internal: softdel %s\n",
306 mod->sm_desc->ad_cname.bv_val, 0, 0);
307 mod->sm_op = LDAP_MOD_DELETE;
309 rc = modify_delete_values( NA->e, mod, get_permissiveModify(op),
310 text, textbuf, textlen );
312 mod->sm_op = SLAP_MOD_SOFTDEL;
314 if ( rc == LDAP_NO_SUCH_ATTRIBUTE) {
318 if( rc != LDAP_SUCCESS ) {
319 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
324 case SLAP_MOD_ADD_IF_NOT_PRESENT:
325 Debug(LDAP_DEBUG_ARGS,
326 "ndb_modify_internal: add_if_not_present %s\n",
327 mod->sm_desc->ad_cname.bv_val, 0, 0);
328 if ( attr_find( NA->e->e_attrs, mod->sm_desc ) ) {
333 mod->sm_op = LDAP_MOD_ADD;
335 rc = modify_add_values( NA->e, mod, get_permissiveModify(op),
336 text, textbuf, textlen );
338 mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
340 if( rc != LDAP_SUCCESS ) {
341 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
347 Debug(LDAP_DEBUG_ANY, "ndb_modify_internal: invalid op %d\n",
349 *text = "Invalid modify operation";
351 Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
355 if ( rc != LDAP_SUCCESS ) {
360 /* If objectClass was modified, reset the flags */
361 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
362 NA->e->e_ocflags = 0;
367 /* check that the entry still obeys the schema */
368 rc = entry_schema_check( op, NA->e, NULL, get_relax(op), 0, NULL,
369 text, textbuf, textlen );
370 if ( rc != LDAP_SUCCESS || op->o_noop ) {
371 if ( rc != LDAP_SUCCESS ) {
372 Debug( LDAP_DEBUG_ANY,
373 "entry failed schema check: %s\n",
380 /* apply modifications to DB */
381 modai = (NdbAttrInfo **)op->o_tmpalloc( nmods * sizeof(NdbAttrInfo*), op->o_tmpmemctx );
383 /* Get the unique list of modified attributes */
384 ldap_pvt_thread_rdwr_rlock( &ni->ni_ai_rwlock );
385 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
386 /* Already took care of objectclass */
387 if ( ml->sml_desc == slap_schema.si_ad_objectClass )
389 for ( i=0; i<nai; i++ ) {
390 if ( ml->sml_desc->ad_type == modai[i]->na_attr )
393 /* This attr was already updated */
396 modai[nai] = ndb_ai_find( ni, ml->sml_desc->ad_type );
397 if ( modai[nai]->na_flag & NDB_INFO_INDEX )
401 ldap_pvt_thread_rdwr_runlock( &ni->ni_ai_rwlock );
403 if ( got_oc || indexed ) {
404 rc = ndb_entry_put_info( op->o_bd, NA, 1 );
411 myDict = NA->ndb->getDictionary();
413 /* sort modai so that OcInfo's are contiguous */
416 for ( i=0; i<nai; i++ ) {
417 for ( j=i+1; j<nai; j++ ) {
418 if ( modai[i]->na_oi == modai[j]->na_oi )
420 for ( k=j+1; k<nai; k++ ) {
421 if ( modai[i]->na_oi == modai[k]->na_oi ) {
428 /* there are no more na_oi's that match modai[i] */
436 /* One call per table... */
437 for ( i=0; i<nai; i += j ) {
439 for ( j=i+1; j<nai; j++ )
440 if ( atmp->na_oi != modai[j]->na_oi )
443 myTable = myDict->getTable( atmp->na_oi->no_table.bv_val );
446 rc = ndb_oc_attrs( NA->txn, myTable, NA->e, atmp->na_oi, &modai[i], j, old );
455 ndb_back_modify( Operation *op, SlapReply *rs )
457 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
459 int manageDSAit = get_manageDSAit( op );
460 char textbuf[SLAP_TEXT_BUFLEN];
461 size_t textlen = sizeof textbuf;
467 struct berval matched;
469 LDAPControl **preread_ctrl = NULL;
470 LDAPControl **postread_ctrl = NULL;
471 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
474 Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(ndb_back_modify) ": %s\n",
475 op->o_req_dn.bv_val, 0, 0 );
477 ctrls[num_ctrls] = NULL;
479 slap_mods_opattrs( op, &op->orm_modlist, 1 );
481 e.e_name = op->o_req_dn;
482 e.e_nname = op->o_req_ndn;
484 /* Get our NDB handle */
485 rs->sr_err = ndb_thread_handle( op, &NA.ndb );
491 retry: /* transaction retry */
495 attrs_free( e.e_attrs );
498 Debug(LDAP_DEBUG_TRACE,
499 LDAP_XSTRING(ndb_back_modify) ": retrying...\n", 0, 0, 0);
500 if ( op->o_abandon ) {
501 rs->sr_err = SLAPD_ABANDON;
505 ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
507 ndb_trans_backoff( ++num_retries );
511 /* begin transaction */
512 NA.txn = NA.ndb->startTransaction();
515 Debug( LDAP_DEBUG_TRACE,
516 LDAP_XSTRING(ndb_back_modify) ": startTransaction failed: %s (%d)\n",
517 NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
518 rs->sr_err = LDAP_OTHER;
519 rs->sr_text = "internal error";
523 /* get entry or ancestor */
524 rs->sr_err = ndb_entry_get_info( op, &NA, 0, &matched );
525 switch( rs->sr_err ) {
528 case LDAP_NO_SUCH_OBJECT:
529 Debug( LDAP_DEBUG_ARGS,
530 "<=- ndb_back_modify: no such object %s\n",
531 op->o_req_dn.bv_val, 0, 0 );
532 rs->sr_matched = matched.bv_val;
534 ndb_check_referral( op, rs, &NA );
537 case DB_LOCK_DEADLOCK:
538 case DB_LOCK_NOTGRANTED:
542 rs->sr_text = "ldap server busy";
545 rs->sr_err = LDAP_OTHER;
546 rs->sr_text = "internal error";
550 /* acquire and lock entry */
551 rs->sr_err = ndb_entry_get_data( op, &NA, 1 );
553 if ( !manageDSAit && is_entry_referral( &e ) ) {
554 /* entry is a referral, don't allow modify */
555 rs->sr_ref = get_entry_referrals( op, &e );
557 Debug( LDAP_DEBUG_TRACE,
558 LDAP_XSTRING(ndb_back_modify) ": entry is referral\n",
561 rs->sr_err = LDAP_REFERRAL;
562 rs->sr_matched = e.e_name.bv_val;
563 rs->sr_flags = REP_REF_MUSTBEFREED;
567 if ( get_assert( op ) &&
568 ( test_filter( op, &e, (Filter*)get_assertion( op )) != LDAP_COMPARE_TRUE ))
570 rs->sr_err = LDAP_ASSERTION_FAILED;
574 if( op->o_preread ) {
575 if( preread_ctrl == NULL ) {
576 preread_ctrl = &ctrls[num_ctrls++];
577 ctrls[num_ctrls] = NULL;
579 if ( slap_read_controls( op, rs, &e,
580 &slap_pre_read_bv, preread_ctrl ) )
582 Debug( LDAP_DEBUG_TRACE,
583 "<=- " LDAP_XSTRING(ndb_back_modify) ": pre-read "
584 "failed!\n", 0, 0, 0 );
585 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
586 /* FIXME: is it correct to abort
587 * operation if control fails? */
593 /* Modify the entry */
594 rs->sr_err = ndb_modify_internal( op, &NA, &rs->sr_text, textbuf, textlen );
596 if( rs->sr_err != LDAP_SUCCESS ) {
597 Debug( LDAP_DEBUG_TRACE,
598 LDAP_XSTRING(ndb_back_modify) ": modify failed (%d)\n",
601 switch( rs->sr_err ) {
602 case DB_LOCK_DEADLOCK:
603 case DB_LOCK_NOTGRANTED:
610 if( op->o_postread ) {
611 if( postread_ctrl == NULL ) {
612 postread_ctrl = &ctrls[num_ctrls++];
613 ctrls[num_ctrls] = NULL;
615 if( slap_read_controls( op, rs, &e,
616 &slap_post_read_bv, postread_ctrl ) )
618 Debug( LDAP_DEBUG_TRACE,
619 "<=- " LDAP_XSTRING(ndb_back_modify)
620 ": post-read failed!\n", 0, 0, 0 );
621 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
622 /* FIXME: is it correct to abort
623 * operation if control fails? */
630 if (( rs->sr_err=NA.txn->execute( NdbTransaction::Rollback,
631 NdbOperation::AbortOnError, 1 )) != 0 ) {
632 rs->sr_text = "txn_abort (no-op) failed";
634 rs->sr_err = LDAP_X_NO_OPERATION;
637 if (( rs->sr_err=NA.txn->execute( NdbTransaction::Commit,
638 NdbOperation::AbortOnError, 1 )) != 0 ) {
639 rs->sr_text = "txn_commit failed";
641 rs->sr_err = LDAP_SUCCESS;
645 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_X_NO_OPERATION ) {
646 Debug( LDAP_DEBUG_TRACE,
647 LDAP_XSTRING(ndb_back_modify) ": txn_%s failed: %s (%d)\n",
648 op->o_noop ? "abort (no-op)" : "commit",
649 NA.txn->getNdbError().message, NA.txn->getNdbError().code );
650 rs->sr_err = LDAP_OTHER;
656 Debug( LDAP_DEBUG_TRACE,
657 LDAP_XSTRING(ndb_back_modify) ": updated%s id=%08lx dn=\"%s\"\n",
658 op->o_noop ? " (no-op)" : "",
659 e.e_id, op->o_req_dn.bv_val );
661 rs->sr_err = LDAP_SUCCESS;
663 if( num_ctrls ) rs->sr_ctrls = ctrls;
667 ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
671 if ( e.e_attrs != NULL ) {
672 attrs_free( e.e_attrs );
676 if( NA.txn != NULL ) {
677 NA.txn->execute( Rollback );
681 send_ldap_result( op, rs );
682 slap_graduate_commit_csn( op );
684 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
685 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
686 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
688 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
689 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
690 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );