1
/* modify.cpp - ndb backend modify routine */
2
/* $OpenLDAP: pkg/ldap/servers/slapd/back-ndb/modify.cpp,v 1.3.2.3 2009/02/05 19:35:54 quanah Exp $ */
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5
* Copyright 2008-2009 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",
304
Debug(LDAP_DEBUG_ANY, "ndb_modify_internal: invalid op %d\n",
306
*text = "Invalid modify operation";
308
Debug(LDAP_DEBUG_ARGS, "ndb_modify_internal: %d %s\n",
312
if ( rc != LDAP_SUCCESS ) {
317
/* If objectClass was modified, reset the flags */
318
if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
319
NA->e->e_ocflags = 0;
324
/* check that the entry still obeys the schema */
325
rc = entry_schema_check( op, NA->e, NULL, get_relax(op), 0, NULL,
326
text, textbuf, textlen );
327
if ( rc != LDAP_SUCCESS || op->o_noop ) {
328
if ( rc != LDAP_SUCCESS ) {
329
Debug( LDAP_DEBUG_ANY,
330
"entry failed schema check: %s\n",
337
/* apply modifications to DB */
338
modai = (NdbAttrInfo **)op->o_tmpalloc( nmods * sizeof(NdbAttrInfo*), op->o_tmpmemctx );
340
/* Get the unique list of modified attributes */
341
ldap_pvt_thread_rdwr_rlock( &ni->ni_ai_rwlock );
342
for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
343
/* Already took care of objectclass */
344
if ( ml->sml_desc == slap_schema.si_ad_objectClass )
346
for ( i=0; i<nai; i++ ) {
347
if ( ml->sml_desc->ad_type == modai[i]->na_attr )
350
/* This attr was already updated */
353
modai[nai] = ndb_ai_find( ni, ml->sml_desc->ad_type );
354
if ( modai[nai]->na_flag & NDB_INFO_INDEX )
358
ldap_pvt_thread_rdwr_runlock( &ni->ni_ai_rwlock );
360
if ( got_oc || indexed ) {
361
rc = ndb_entry_put_info( op->o_bd, NA, 1 );
368
myDict = NA->ndb->getDictionary();
370
/* sort modai so that OcInfo's are contiguous */
373
for ( i=0; i<nai; i++ ) {
374
for ( j=i+1; j<nai; j++ ) {
375
if ( modai[i]->na_oi == modai[j]->na_oi )
377
for ( k=j+1; k<nai; k++ ) {
378
if ( modai[i]->na_oi == modai[k]->na_oi ) {
385
/* there are no more na_oi's that match modai[i] */
393
/* One call per table... */
394
for ( i=0; i<nai; i += j ) {
396
for ( j=i+1; j<nai; j++ )
397
if ( atmp->na_oi != modai[j]->na_oi )
400
myTable = myDict->getTable( atmp->na_oi->no_table.bv_val );
403
rc = ndb_oc_attrs( NA->txn, myTable, NA->e, atmp->na_oi, &modai[i], j, old );
412
ndb_back_modify( Operation *op, SlapReply *rs )
414
struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
416
int manageDSAit = get_manageDSAit( op );
417
char textbuf[SLAP_TEXT_BUFLEN];
418
size_t textlen = sizeof textbuf;
424
struct berval matched;
426
LDAPControl **preread_ctrl = NULL;
427
LDAPControl **postread_ctrl = NULL;
428
LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
431
Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(ndb_back_modify) ": %s\n",
432
op->o_req_dn.bv_val, 0, 0 );
434
ctrls[num_ctrls] = NULL;
436
slap_mods_opattrs( op, &op->orm_modlist, 1 );
438
e.e_name = op->o_req_dn;
439
e.e_nname = op->o_req_ndn;
441
/* Get our NDB handle */
442
rs->sr_err = ndb_thread_handle( op, &NA.ndb );
448
retry: /* transaction retry */
452
attrs_free( e.e_attrs );
455
Debug(LDAP_DEBUG_TRACE,
456
LDAP_XSTRING(ndb_back_modify) ": retrying...\n", 0, 0, 0);
457
if ( op->o_abandon ) {
458
rs->sr_err = SLAPD_ABANDON;
462
ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
464
ndb_trans_backoff( ++num_retries );
468
/* begin transaction */
469
NA.txn = NA.ndb->startTransaction();
472
Debug( LDAP_DEBUG_TRACE,
473
LDAP_XSTRING(ndb_back_modify) ": startTransaction failed: %s (%d)\n",
474
NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
475
rs->sr_err = LDAP_OTHER;
476
rs->sr_text = "internal error";
480
/* get entry or ancestor */
481
rs->sr_err = ndb_entry_get_info( op, &NA, 0, &matched );
482
switch( rs->sr_err ) {
485
case LDAP_NO_SUCH_OBJECT:
486
Debug( LDAP_DEBUG_ARGS,
487
"<=- ndb_back_modify: no such object %s\n",
488
op->o_req_dn.bv_val, 0, 0 );
489
rs->sr_matched = matched.bv_val;
491
ndb_check_referral( op, rs, &NA );
494
case DB_LOCK_DEADLOCK:
495
case DB_LOCK_NOTGRANTED:
499
rs->sr_text = "ldap server busy";
502
rs->sr_err = LDAP_OTHER;
503
rs->sr_text = "internal error";
507
/* acquire and lock entry */
508
rs->sr_err = ndb_entry_get_data( op, &NA, 1 );
510
if ( !manageDSAit && is_entry_referral( &e ) ) {
511
/* entry is a referral, don't allow modify */
512
rs->sr_ref = get_entry_referrals( op, &e );
514
Debug( LDAP_DEBUG_TRACE,
515
LDAP_XSTRING(ndb_back_modify) ": entry is referral\n",
518
rs->sr_err = LDAP_REFERRAL;
519
rs->sr_matched = e.e_name.bv_val;
520
rs->sr_flags = REP_REF_MUSTBEFREED;
524
if ( get_assert( op ) &&
525
( test_filter( op, &e, (Filter*)get_assertion( op )) != LDAP_COMPARE_TRUE ))
527
rs->sr_err = LDAP_ASSERTION_FAILED;
531
if( op->o_preread ) {
532
if( preread_ctrl == NULL ) {
533
preread_ctrl = &ctrls[num_ctrls++];
534
ctrls[num_ctrls] = NULL;
536
if ( slap_read_controls( op, rs, &e,
537
&slap_pre_read_bv, preread_ctrl ) )
539
Debug( LDAP_DEBUG_TRACE,
540
"<=- " LDAP_XSTRING(ndb_back_modify) ": pre-read "
541
"failed!\n", 0, 0, 0 );
542
if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
543
/* FIXME: is it correct to abort
544
* operation if control fails? */
550
/* Modify the entry */
551
rs->sr_err = ndb_modify_internal( op, &NA, &rs->sr_text, textbuf, textlen );
553
if( rs->sr_err != LDAP_SUCCESS ) {
554
Debug( LDAP_DEBUG_TRACE,
555
LDAP_XSTRING(ndb_back_modify) ": modify failed (%d)\n",
558
switch( rs->sr_err ) {
559
case DB_LOCK_DEADLOCK:
560
case DB_LOCK_NOTGRANTED:
567
if( op->o_postread ) {
568
if( postread_ctrl == NULL ) {
569
postread_ctrl = &ctrls[num_ctrls++];
570
ctrls[num_ctrls] = NULL;
572
if( slap_read_controls( op, rs, &e,
573
&slap_post_read_bv, postread_ctrl ) )
575
Debug( LDAP_DEBUG_TRACE,
576
"<=- " LDAP_XSTRING(ndb_back_modify)
577
": post-read failed!\n", 0, 0, 0 );
578
if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
579
/* FIXME: is it correct to abort
580
* operation if control fails? */
587
if (( rs->sr_err=NA.txn->execute( NdbTransaction::Rollback,
588
NdbOperation::AbortOnError, 1 )) != 0 ) {
589
rs->sr_text = "txn_abort (no-op) failed";
591
rs->sr_err = LDAP_X_NO_OPERATION;
594
if (( rs->sr_err=NA.txn->execute( NdbTransaction::Commit,
595
NdbOperation::AbortOnError, 1 )) != 0 ) {
596
rs->sr_text = "txn_commit failed";
598
rs->sr_err = LDAP_SUCCESS;
602
if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_X_NO_OPERATION ) {
603
Debug( LDAP_DEBUG_TRACE,
604
LDAP_XSTRING(ndb_back_modify) ": txn_%s failed: %s (%d)\n",
605
op->o_noop ? "abort (no-op)" : "commit",
606
NA.txn->getNdbError().message, NA.txn->getNdbError().code );
607
rs->sr_err = LDAP_OTHER;
613
Debug( LDAP_DEBUG_TRACE,
614
LDAP_XSTRING(ndb_back_modify) ": updated%s id=%08lx dn=\"%s\"\n",
615
op->o_noop ? " (no-op)" : "",
616
e.e_id, op->o_req_dn.bv_val );
618
rs->sr_err = LDAP_SUCCESS;
620
if( num_ctrls ) rs->sr_ctrls = ctrls;
624
ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
628
if ( e.e_attrs != NULL ) {
629
attrs_free( e.e_attrs );
633
if( NA.txn != NULL ) {
634
NA.txn->execute( Rollback );
638
send_ldap_result( op, rs );
639
slap_graduate_commit_csn( op );
641
if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
642
slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
643
slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
645
if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
646
slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
647
slap_sl_free( *postread_ctrl, op->o_tmpmemctx );