]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modify.c
Happy New Year!
[openldap] / servers / slapd / back-bdb / modify.c
1 /* modify.c - bdb backend modify routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2012 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/time.h>
22
23 #include "back-bdb.h"
24
25 static struct berval scbva[] = {
26         BER_BVC("glue"),
27         BER_BVNULL
28 };
29
30 static void
31 bdb_modify_idxflags(
32         Operation *op,
33         AttributeDescription *desc,
34         int got_delete,
35         Attribute *newattrs,
36         Attribute *oldattrs )
37 {
38         struct berval   ix_at;
39         AttrInfo        *ai;
40
41         /* check if modified attribute was indexed
42          * but not in case of NOOP... */
43         ai = bdb_index_mask( op->o_bd, desc, &ix_at );
44         if ( ai ) {
45                 if ( got_delete ) {
46                         Attribute       *ap;
47                         struct berval   ix2;
48
49                         ap = attr_find( oldattrs, desc );
50                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXDEL;
51
52                         /* Find all other attrs that index to same slot */
53                         for ( ap = newattrs; ap; ap = ap->a_next ) {
54                                 ai = bdb_index_mask( op->o_bd, ap->a_desc, &ix2 );
55                                 if ( ai && ix2.bv_val == ix_at.bv_val )
56                                         ap->a_flags |= SLAP_ATTR_IXADD;
57                         }
58
59                 } else {
60                         Attribute       *ap;
61
62                         ap = attr_find( newattrs, desc );
63                         if ( ap ) ap->a_flags |= SLAP_ATTR_IXADD;
64                 }
65         }
66 }
67
68 int bdb_modify_internal(
69         Operation *op,
70         DB_TXN *tid,
71         Modifications *modlist,
72         Entry *e,
73         const char **text,
74         char *textbuf,
75         size_t textlen )
76 {
77         int rc, err;
78         Modification    *mod;
79         Modifications   *ml;
80         Attribute       *save_attrs;
81         Attribute       *ap;
82         int                     glue_attr_delete = 0;
83         int                     got_delete;
84
85         Debug( LDAP_DEBUG_TRACE, "bdb_modify_internal: 0x%08lx: %s\n",
86                 e->e_id, e->e_dn, 0);
87
88         if ( !acl_check_modlist( op, e, modlist )) {
89                 return LDAP_INSUFFICIENT_ACCESS;
90         }
91
92         /* save_attrs will be disposed of by bdb_cache_modify */
93         save_attrs = e->e_attrs;
94         e->e_attrs = attrs_dup( e->e_attrs );
95
96         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
97                 int match;
98                 mod = &ml->sml_mod;
99                 switch( mod->sm_op ) {
100                 case LDAP_MOD_ADD:
101                 case LDAP_MOD_REPLACE:
102                         if ( mod->sm_desc == slap_schema.si_ad_structuralObjectClass ) {
103                                 value_match( &match, slap_schema.si_ad_structuralObjectClass,
104                                         slap_schema.si_ad_structuralObjectClass->
105                                                 ad_type->sat_equality,
106                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
107                                         &mod->sm_values[0], &scbva[0], text );
108                                 if ( !match ) glue_attr_delete = 1;
109                         }
110                 }
111                 if ( glue_attr_delete )
112                         break;
113         }
114
115         if ( glue_attr_delete ) {
116                 Attribute       **app = &e->e_attrs;
117                 while ( *app != NULL ) {
118                         if ( !is_at_operational( (*app)->a_desc->ad_type )) {
119                                 Attribute *save = *app;
120                                 *app = (*app)->a_next;
121                                 attr_free( save );
122                                 continue;
123                         }
124                         app = &(*app)->a_next;
125                 }
126         }
127
128         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
129                 mod = &ml->sml_mod;
130                 got_delete = 0;
131
132                 switch ( mod->sm_op ) {
133                 case LDAP_MOD_ADD:
134                         Debug(LDAP_DEBUG_ARGS,
135                                 "bdb_modify_internal: add %s\n",
136                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
137                         err = modify_add_values( e, mod, get_permissiveModify(op),
138                                 text, textbuf, textlen );
139                         if( err != LDAP_SUCCESS ) {
140                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
141                                         err, *text, 0);
142                         }
143                         break;
144
145                 case LDAP_MOD_DELETE:
146                         if ( glue_attr_delete ) {
147                                 err = LDAP_SUCCESS;
148                                 break;
149                         }
150
151                         Debug(LDAP_DEBUG_ARGS,
152                                 "bdb_modify_internal: delete %s\n",
153                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
154                         err = modify_delete_values( e, mod, get_permissiveModify(op),
155                                 text, textbuf, textlen );
156                         if( err != LDAP_SUCCESS ) {
157                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
158                                         err, *text, 0);
159                         } else {
160                                 got_delete = 1;
161                         }
162                         break;
163
164                 case LDAP_MOD_REPLACE:
165                         Debug(LDAP_DEBUG_ARGS,
166                                 "bdb_modify_internal: replace %s\n",
167                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
168                         err = modify_replace_values( e, mod, get_permissiveModify(op),
169                                 text, textbuf, textlen );
170                         if( err != LDAP_SUCCESS ) {
171                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
172                                         err, *text, 0);
173                         } else {
174                                 got_delete = 1;
175                         }
176                         break;
177
178                 case LDAP_MOD_INCREMENT:
179                         Debug(LDAP_DEBUG_ARGS,
180                                 "bdb_modify_internal: increment %s\n",
181                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
182                         err = modify_increment_values( e, mod, get_permissiveModify(op),
183                                 text, textbuf, textlen );
184                         if( err != LDAP_SUCCESS ) {
185                                 Debug(LDAP_DEBUG_ARGS,
186                                         "bdb_modify_internal: %d %s\n",
187                                         err, *text, 0);
188                         } else {
189                                 got_delete = 1;
190                         }
191                         break;
192
193                 case SLAP_MOD_SOFTADD:
194                         Debug(LDAP_DEBUG_ARGS,
195                                 "bdb_modify_internal: softadd %s\n",
196                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
197                         /* Avoid problems in index_add_mods()
198                          * We need to add index if necessary.
199                          */
200                         mod->sm_op = LDAP_MOD_ADD;
201
202                         err = modify_add_values( e, mod, get_permissiveModify(op),
203                                 text, textbuf, textlen );
204
205                         mod->sm_op = SLAP_MOD_SOFTADD;
206
207                         if ( err == LDAP_TYPE_OR_VALUE_EXISTS ) {
208                                 err = LDAP_SUCCESS;
209                         }
210
211                         if( err != LDAP_SUCCESS ) {
212                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
213                                         err, *text, 0);
214                         }
215                         break;
216
217                 case SLAP_MOD_SOFTDEL:
218                         Debug(LDAP_DEBUG_ARGS,
219                                 "bdb_modify_internal: softdel %s\n",
220                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
221                         /* Avoid problems in index_delete_mods()
222                          * We need to add index if necessary.
223                          */
224                         mod->sm_op = LDAP_MOD_DELETE;
225
226                         err = modify_delete_values( e, mod, get_permissiveModify(op),
227                                 text, textbuf, textlen );
228
229                         mod->sm_op = SLAP_MOD_SOFTDEL;
230
231                         if ( err == LDAP_NO_SUCH_ATTRIBUTE ) {
232                                 err = LDAP_SUCCESS;
233                         }
234
235                         if( err != LDAP_SUCCESS ) {
236                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
237                                         err, *text, 0);
238                         }
239                         break;
240
241                 case SLAP_MOD_ADD_IF_NOT_PRESENT:
242                         if ( attr_find( e->e_attrs, mod->sm_desc ) != NULL ) {
243                                 /* skip */
244                                 err = LDAP_SUCCESS;
245                                 break;
246                         }
247
248                         Debug(LDAP_DEBUG_ARGS,
249                                 "bdb_modify_internal: add_if_not_present %s\n",
250                                 mod->sm_desc->ad_cname.bv_val, 0, 0);
251                         /* Avoid problems in index_add_mods()
252                          * We need to add index if necessary.
253                          */
254                         mod->sm_op = LDAP_MOD_ADD;
255
256                         err = modify_add_values( e, mod, get_permissiveModify(op),
257                                 text, textbuf, textlen );
258
259                         mod->sm_op = SLAP_MOD_ADD_IF_NOT_PRESENT;
260
261                         if( err != LDAP_SUCCESS ) {
262                                 Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
263                                         err, *text, 0);
264                         }
265                         break;
266
267                 default:
268                         Debug(LDAP_DEBUG_ANY, "bdb_modify_internal: invalid op %d\n",
269                                 mod->sm_op, 0, 0);
270                         *text = "Invalid modify operation";
271                         err = LDAP_OTHER;
272                         Debug(LDAP_DEBUG_ARGS, "bdb_modify_internal: %d %s\n",
273                                 err, *text, 0);
274                 }
275
276                 if ( err != LDAP_SUCCESS ) {
277                         attrs_free( e->e_attrs );
278                         e->e_attrs = save_attrs;
279                         /* unlock entry, delete from cache */
280                         return err; 
281                 }
282
283                 /* If objectClass was modified, reset the flags */
284                 if ( mod->sm_desc == slap_schema.si_ad_objectClass ) {
285                         e->e_ocflags = 0;
286                 }
287
288                 if ( glue_attr_delete ) e->e_ocflags = 0;
289
290
291                 /* check if modified attribute was indexed
292                  * but not in case of NOOP... */
293                 if ( !op->o_noop ) {
294                         bdb_modify_idxflags( op, mod->sm_desc, got_delete, e->e_attrs, save_attrs );
295                 }
296         }
297
298         /* check that the entry still obeys the schema */
299         ap = NULL;
300         rc = entry_schema_check( op, e, save_attrs, get_relax(op), 0, &ap,
301                 text, textbuf, textlen );
302         if ( rc != LDAP_SUCCESS || op->o_noop ) {
303                 attrs_free( e->e_attrs );
304                 /* clear the indexing flags */
305                 for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
306                         ap->a_flags &= ~(SLAP_ATTR_IXADD|SLAP_ATTR_IXDEL);
307                 }
308                 e->e_attrs = save_attrs;
309
310                 if ( rc != LDAP_SUCCESS ) {
311                         Debug( LDAP_DEBUG_ANY,
312                                 "entry failed schema check: %s\n",
313                                 *text, 0, 0 );
314                 }
315
316                 /* if NOOP then silently revert to saved attrs */
317                 return rc;
318         }
319
320         /* structuralObjectClass modified! */
321         if ( ap ) {
322                 assert( ap->a_desc == slap_schema.si_ad_structuralObjectClass );
323                 if ( !op->o_noop ) {
324                         bdb_modify_idxflags( op, slap_schema.si_ad_structuralObjectClass,
325                                 1, e->e_attrs, save_attrs );
326                 }
327         }
328
329         /* update the indices of the modified attributes */
330
331         /* start with deleting the old index entries */
332         for ( ap = save_attrs; ap != NULL; ap = ap->a_next ) {
333                 if ( ap->a_flags & SLAP_ATTR_IXDEL ) {
334                         struct berval *vals;
335                         Attribute *a2;
336                         ap->a_flags &= ~SLAP_ATTR_IXDEL;
337                         a2 = attr_find( e->e_attrs, ap->a_desc );
338                         if ( a2 ) {
339                                 /* need to detect which values were deleted */
340                                 int i, j;
341                                 vals = op->o_tmpalloc( (ap->a_numvals + 1) *
342                                         sizeof(struct berval), op->o_tmpmemctx );
343                                 j = 0;
344                                 for ( i=0; i < ap->a_numvals; i++ ) {
345                                         rc = attr_valfind( a2, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
346                                                 &ap->a_nvals[i], NULL, op->o_tmpmemctx );
347                                         /* Save deleted values */
348                                         if ( rc == LDAP_NO_SUCH_ATTRIBUTE )
349                                                 vals[j++] = ap->a_nvals[i];
350                                 }
351                                 BER_BVZERO(vals+j);
352                         } else {
353                                 /* attribute was completely deleted */
354                                 vals = ap->a_nvals;
355                         }
356                         rc = 0;
357                         if ( !BER_BVISNULL( vals )) {
358                                 rc = bdb_index_values( op, tid, ap->a_desc,
359                                         vals, e->e_id, SLAP_INDEX_DELETE_OP );
360                                 if ( rc != LDAP_SUCCESS ) {
361                                         Debug( LDAP_DEBUG_ANY,
362                                                 "%s: attribute \"%s\" index delete failure\n",
363                                                 op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
364                                         attrs_free( e->e_attrs );
365                                         e->e_attrs = save_attrs;
366                                 }
367                         }
368                         if ( vals != ap->a_nvals )
369                                 op->o_tmpfree( vals, op->o_tmpmemctx );
370                         if ( rc ) return rc;
371                 }
372         }
373
374         /* add the new index entries */
375         for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
376                 if (ap->a_flags & SLAP_ATTR_IXADD) {
377                         ap->a_flags &= ~SLAP_ATTR_IXADD;
378                         rc = bdb_index_values( op, tid, ap->a_desc,
379                                 ap->a_nvals,
380                                 e->e_id, SLAP_INDEX_ADD_OP );
381                         if ( rc != LDAP_SUCCESS ) {
382                                 Debug( LDAP_DEBUG_ANY,
383                                        "%s: attribute \"%s\" index add failure\n",
384                                         op->o_log_prefix, ap->a_desc->ad_cname.bv_val, 0 );
385                                 attrs_free( e->e_attrs );
386                                 e->e_attrs = save_attrs;
387                                 return rc;
388                         }
389                 }
390         }
391
392         return rc;
393 }
394
395
396 int
397 bdb_modify( Operation *op, SlapReply *rs )
398 {
399         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
400         Entry           *e = NULL;
401         EntryInfo       *ei = NULL;
402         int             manageDSAit = get_manageDSAit( op );
403         char textbuf[SLAP_TEXT_BUFLEN];
404         size_t textlen = sizeof textbuf;
405         DB_TXN  *ltid = NULL, *lt2;
406         struct bdb_op_info opinfo = {{{ 0 }}};
407         Entry           dummy = {0};
408
409         DB_LOCK         lock;
410
411         int             num_retries = 0;
412
413         LDAPControl **preread_ctrl = NULL;
414         LDAPControl **postread_ctrl = NULL;
415         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
416         int num_ctrls = 0;
417
418         int rc;
419
420 #ifdef LDAP_X_TXN
421         int settle = 0;
422 #endif
423
424         Debug( LDAP_DEBUG_ARGS, LDAP_XSTRING(bdb_modify) ": %s\n",
425                 op->o_req_dn.bv_val, 0, 0 );
426
427 #ifdef LDAP_X_TXN
428         if( op->o_txnSpec ) {
429                 /* acquire connection lock */
430                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
431                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
432                         rs->sr_text = "invalid transaction identifier";
433                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
434                         goto txnReturn;
435                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
436                         settle=1;
437                         goto txnReturn;
438                 }
439
440                 if( op->o_conn->c_txn_backend == NULL ) {
441                         op->o_conn->c_txn_backend = op->o_bd;
442
443                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
444                         rs->sr_text = "transaction cannot span multiple database contexts";
445                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
446                         goto txnReturn;
447                 }
448
449                 /* insert operation into transaction */
450
451                 rs->sr_text = "transaction specified";
452                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
453
454 txnReturn:
455                 /* release connection lock */
456                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
457
458                 if( !settle ) {
459                         send_ldap_result( op, rs );
460                         return rs->sr_err;
461                 }
462         }
463 #endif
464
465         ctrls[num_ctrls] = NULL;
466
467         /* Don't touch the opattrs, if this is a contextCSN update
468          * initiated from updatedn */
469         if ( !be_isupdate(op) || !op->orm_modlist || op->orm_modlist->sml_next ||
470                  op->orm_modlist->sml_desc != slap_schema.si_ad_contextCSN ) {
471
472                 slap_mods_opattrs( op, &op->orm_modlist, 1 );
473         }
474
475         if( 0 ) {
476 retry:  /* transaction retry */
477                 if ( dummy.e_attrs ) {
478                         attrs_free( dummy.e_attrs );
479                         dummy.e_attrs = NULL;
480                 }
481                 if( e != NULL ) {
482                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
483                         e = NULL;
484                 }
485                 Debug(LDAP_DEBUG_TRACE,
486                         LDAP_XSTRING(bdb_modify) ": retrying...\n", 0, 0, 0);
487
488                 rs->sr_err = TXN_ABORT( ltid );
489                 ltid = NULL;
490                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
491                 opinfo.boi_oe.oe_key = NULL;
492                 op->o_do_not_cache = opinfo.boi_acl_cache;
493                 if( rs->sr_err != 0 ) {
494                         rs->sr_err = LDAP_OTHER;
495                         rs->sr_text = "internal error";
496                         goto return_results;
497                 }
498                 if ( op->o_abandon ) {
499                         rs->sr_err = SLAPD_ABANDON;
500                         goto return_results;
501                 }
502                 bdb_trans_backoff( ++num_retries );
503         }
504
505         /* begin transaction */
506         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
507                 bdb->bi_db_opflags );
508         rs->sr_text = NULL;
509         if( rs->sr_err != 0 ) {
510                 Debug( LDAP_DEBUG_TRACE,
511                         LDAP_XSTRING(bdb_modify) ": txn_begin failed: "
512                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
513                 rs->sr_err = LDAP_OTHER;
514                 rs->sr_text = "internal error";
515                 goto return_results;
516         }
517
518         opinfo.boi_oe.oe_key = bdb;
519         opinfo.boi_txn = ltid;
520         opinfo.boi_err = 0;
521         opinfo.boi_acl_cache = op->o_do_not_cache;
522         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
523
524         /* get entry or ancestor */
525         rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
526                 &lock );
527
528         if ( rs->sr_err != 0 ) {
529                 Debug( LDAP_DEBUG_TRACE,
530                         LDAP_XSTRING(bdb_modify) ": dn2entry failed (%d)\n",
531                         rs->sr_err, 0, 0 );
532                 switch( rs->sr_err ) {
533                 case DB_LOCK_DEADLOCK:
534                 case DB_LOCK_NOTGRANTED:
535                         goto retry;
536                 case DB_NOTFOUND:
537                         break;
538                 case LDAP_BUSY:
539                         rs->sr_text = "ldap server busy";
540                         goto return_results;
541                 default:
542                         rs->sr_err = LDAP_OTHER;
543                         rs->sr_text = "internal error";
544                         goto return_results;
545                 }
546         }
547
548         e = ei->bei_e;
549
550         /* acquire and lock entry */
551         /* FIXME: dn2entry() should return non-glue entry */
552         if (( rs->sr_err == DB_NOTFOUND ) ||
553                 ( !manageDSAit && e && is_entry_glue( e )))
554         {
555                 if ( e != NULL ) {
556                         rs->sr_matched = ch_strdup( e->e_dn );
557                         rs->sr_ref = is_entry_referral( e )
558                                 ? get_entry_referrals( op, e )
559                                 : NULL;
560                         bdb_unlocked_cache_return_entry_r (&bdb->bi_cache, e);
561                         e = NULL;
562
563                 } else {
564                         rs->sr_ref = referral_rewrite( default_referral, NULL,
565                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
566                 }
567
568                 rs->sr_err = LDAP_REFERRAL;
569                 send_ldap_result( op, rs );
570
571                 if ( rs->sr_ref != default_referral ) {
572                         ber_bvarray_free( rs->sr_ref );
573                 }
574                 free( (char *)rs->sr_matched );
575                 rs->sr_ref = NULL;
576                 rs->sr_matched = NULL;
577
578                 goto done;
579         }
580
581         if ( !manageDSAit && is_entry_referral( e ) ) {
582                 /* entry is a referral, don't allow modify */
583                 rs->sr_ref = get_entry_referrals( op, e );
584
585                 Debug( LDAP_DEBUG_TRACE,
586                         LDAP_XSTRING(bdb_modify) ": entry is referral\n",
587                         0, 0, 0 );
588
589                 rs->sr_err = LDAP_REFERRAL;
590                 rs->sr_matched = e->e_name.bv_val;
591                 send_ldap_result( op, rs );
592
593                 ber_bvarray_free( rs->sr_ref );
594                 rs->sr_ref = NULL;
595                 rs->sr_matched = NULL;
596                 goto done;
597         }
598
599         if ( get_assert( op ) &&
600                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
601         {
602                 rs->sr_err = LDAP_ASSERTION_FAILED;
603                 goto return_results;
604         }
605
606         if( op->o_preread ) {
607                 if( preread_ctrl == NULL ) {
608                         preread_ctrl = &ctrls[num_ctrls++];
609                         ctrls[num_ctrls] = NULL;
610                 }
611                 if ( slap_read_controls( op, rs, e,
612                         &slap_pre_read_bv, preread_ctrl ) )
613                 {
614                         Debug( LDAP_DEBUG_TRACE,
615                                 "<=- " LDAP_XSTRING(bdb_modify) ": pre-read "
616                                 "failed!\n", 0, 0, 0 );
617                         if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
618                                 /* FIXME: is it correct to abort
619                                  * operation if control fails? */
620                                 goto return_results;
621                         }
622                 }
623         }
624
625         /* nested transaction */
626         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
627         rs->sr_text = NULL;
628         if( rs->sr_err != 0 ) {
629                 Debug( LDAP_DEBUG_TRACE,
630                         LDAP_XSTRING(bdb_modify) ": txn_begin(2) failed: " "%s (%d)\n",
631                         db_strerror(rs->sr_err), rs->sr_err, 0 );
632                 rs->sr_err = LDAP_OTHER;
633                 rs->sr_text = "internal error";
634                 goto return_results;
635         }
636         /* Modify the entry */
637         dummy = *e;
638         rs->sr_err = bdb_modify_internal( op, lt2, op->orm_modlist,
639                 &dummy, &rs->sr_text, textbuf, textlen );
640
641         if( rs->sr_err != LDAP_SUCCESS ) {
642                 Debug( LDAP_DEBUG_TRACE,
643                         LDAP_XSTRING(bdb_modify) ": modify failed (%d)\n",
644                         rs->sr_err, 0, 0 );
645                 if ( (rs->sr_err == LDAP_INSUFFICIENT_ACCESS) && opinfo.boi_err ) {
646                         rs->sr_err = opinfo.boi_err;
647                 }
648                 /* Only free attrs if they were dup'd.  */
649                 if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
650                 switch( rs->sr_err ) {
651                 case DB_LOCK_DEADLOCK:
652                 case DB_LOCK_NOTGRANTED:
653                         goto retry;
654                 }
655                 goto return_results;
656         }
657
658         /* change the entry itself */
659         rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
660         if ( rs->sr_err != 0 ) {
661                 Debug( LDAP_DEBUG_TRACE,
662                         LDAP_XSTRING(bdb_modify) ": id2entry update failed " "(%d)\n",
663                         rs->sr_err, 0, 0 );
664                 switch( rs->sr_err ) {
665                 case DB_LOCK_DEADLOCK:
666                 case DB_LOCK_NOTGRANTED:
667                         goto retry;
668                 }
669                 rs->sr_text = "entry update failed";
670                 goto return_results;
671         }
672
673         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
674                 rs->sr_err = LDAP_OTHER;
675                 rs->sr_text = "txn_commit(2) failed";
676                 goto return_results;
677         }
678
679         if( op->o_postread ) {
680                 if( postread_ctrl == NULL ) {
681                         postread_ctrl = &ctrls[num_ctrls++];
682                         ctrls[num_ctrls] = NULL;
683                 }
684                 if( slap_read_controls( op, rs, &dummy,
685                         &slap_post_read_bv, postread_ctrl ) )
686                 {
687                         Debug( LDAP_DEBUG_TRACE,
688                                 "<=- " LDAP_XSTRING(bdb_modify)
689                                 ": post-read failed!\n", 0, 0, 0 );
690                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
691                                 /* FIXME: is it correct to abort
692                                  * operation if control fails? */
693                                 goto return_results;
694                         }
695                 }
696         }
697
698         if( op->o_noop ) {
699                 if ( ( rs->sr_err = TXN_ABORT( ltid ) ) != 0 ) {
700                         rs->sr_text = "txn_abort (no-op) failed";
701                 } else {
702                         rs->sr_err = LDAP_X_NO_OPERATION;
703                         ltid = NULL;
704                         /* Only free attrs if they were dup'd.  */
705                         if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
706                         goto return_results;
707                 }
708         } else {
709                 /* may have changed in bdb_modify_internal() */
710                 e->e_ocflags = dummy.e_ocflags;
711                 rc = bdb_cache_modify( bdb, e, dummy.e_attrs, ltid, &lock );
712                 switch( rc ) {
713                 case DB_LOCK_DEADLOCK:
714                 case DB_LOCK_NOTGRANTED:
715                         goto retry;
716                 }
717                 dummy.e_attrs = NULL;
718
719                 rs->sr_err = TXN_COMMIT( ltid, 0 );
720         }
721         ltid = NULL;
722         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
723         opinfo.boi_oe.oe_key = NULL;
724
725         if( rs->sr_err != 0 ) {
726                 Debug( LDAP_DEBUG_TRACE,
727                         LDAP_XSTRING(bdb_modify) ": txn_%s failed: %s (%d)\n",
728                         op->o_noop ? "abort (no-op)" : "commit",
729                         db_strerror(rs->sr_err), rs->sr_err );
730                 rs->sr_err = LDAP_OTHER;
731                 rs->sr_text = "commit failed";
732
733                 goto return_results;
734         }
735
736         Debug( LDAP_DEBUG_TRACE,
737                 LDAP_XSTRING(bdb_modify) ": updated%s id=%08lx dn=\"%s\"\n",
738                 op->o_noop ? " (no-op)" : "",
739                 dummy.e_id, op->o_req_dn.bv_val );
740
741         rs->sr_err = LDAP_SUCCESS;
742         rs->sr_text = NULL;
743         if( num_ctrls ) rs->sr_ctrls = ctrls;
744
745 return_results:
746         if( dummy.e_attrs ) {
747                 attrs_free( dummy.e_attrs );
748         }
749         send_ldap_result( op, rs );
750
751         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
752                 TXN_CHECKPOINT( bdb->bi_dbenv,
753                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
754         }
755
756 done:
757         slap_graduate_commit_csn( op );
758
759         if( ltid != NULL ) {
760                 TXN_ABORT( ltid );
761         }
762         if ( opinfo.boi_oe.oe_key ) {
763                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
764         }
765
766         if( e != NULL ) {
767                 bdb_unlocked_cache_return_entry_w (&bdb->bi_cache, e);
768         }
769
770         if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
771                 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
772                 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
773         }
774         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
775                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
776                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
777         }
778
779         rs->sr_text = NULL;
780
781         return rs->sr_err;
782 }