]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
d6883865c4c8b1a131135464e61f9e75f9d9b67a
[openldap] / servers / slapd / back-bdb / modrdn.c
1 /* modrdn.c - bdb backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 static char bdb_gid[DB_XIDDATASIZE];
17
18 int
19 bdb_modrdn(
20         Backend *be,
21         Connection      *conn,
22         Operation       *op,
23         struct berval   *dn,
24         struct berval   *ndn,
25         struct berval   *newrdn,
26         struct berval   *nnewrdn,
27         int             deleteoldrdn,
28         struct berval   *newSuperior,
29         struct berval   *nnewSuperior )
30 {
31         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
32         AttributeDescription *children = slap_schema.si_ad_children;
33         struct berval   p_dn, p_ndn;
34         struct berval   new_dn = {0, NULL}, new_ndn = {0, NULL};
35         int             isroot = -1;
36         Entry           *e, *p = NULL;
37         Entry           *matched;
38         int                     rc;
39         const char *text;
40         char textbuf[SLAP_TEXT_BUFLEN];
41         size_t textlen = sizeof textbuf;
42         DB_TXN *        ltid = NULL;
43         struct bdb_op_info opinfo;
44
45         ID                      id;
46         int             a_cnt, d_cnt;
47         LDAPRDN         *new_rdn = NULL;
48         LDAPRDN         *old_rdn = NULL;
49
50         Entry           *np = NULL;                     /* newSuperior Entry */
51         struct berval   *np_dn = NULL;                  /* newSuperior dn */
52         struct berval   *np_ndn = NULL;                 /* newSuperior ndn */
53         struct berval   *new_parent_dn = NULL;  /* np_dn, p_dn, or NULL */
54
55         /* Used to interface with bdb_modify_internal() */
56         Modifications   *mod = NULL;            /* Used to delete old rdn */
57
58         int             manageDSAit = get_manageDSAit( op );
59
60         Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
61                 dn->bv_val, newrdn->bv_val,
62                 newSuperior ? newSuperior->bv_val : "NULL" );
63
64 #if 0
65         if( newSuperior != NULL ) {
66                 rc = LDAP_UNWILLING_TO_PERFORM;
67                 text = "newSuperior not implemented (yet)";
68                 goto return_results;
69         }
70 #endif
71
72         if( 0 ) {
73 retry:  /* transaction retry */
74                 if (e != NULL) {
75                         bdb_cache_delete_entry(&bdb->bi_cache, e);
76                         bdb_cache_return_entry_w(&bdb->bi_cache, e);
77                 }
78                 if (p != NULL) {
79                         bdb_cache_return_entry_r(&bdb->bi_cache, p);
80                 }
81                 if (np != NULL) {
82                         bdb_cache_return_entry_r(&bdb->bi_cache, np);
83                 }
84                 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
85                 rc = TXN_ABORT( ltid );
86                 ltid = NULL;
87                 op->o_private = NULL;
88                 if( rc != 0 ) {
89                         rc = LDAP_OTHER;
90                         text = "internal error";
91                         goto return_results;
92                 }
93                 ldap_pvt_thread_yield();
94         }
95
96         /* begin transaction */
97         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
98                 bdb->bi_db_opflags );
99         text = NULL;
100         if( rc != 0 ) {
101                 Debug( LDAP_DEBUG_TRACE,
102                         "bdb_delete: txn_begin failed: %s (%d)\n",
103                         db_strerror(rc), rc, 0 );
104                 rc = LDAP_OTHER;
105                 text = "internal error";
106                 goto return_results;
107         }
108
109         opinfo.boi_bdb = be;
110         opinfo.boi_txn = ltid;
111         opinfo.boi_err = 0;
112         op->o_private = &opinfo;
113
114         /* get entry */
115         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
116
117         switch( rc ) {
118         case 0:
119         case DB_NOTFOUND:
120                 break;
121         case DB_LOCK_DEADLOCK:
122         case DB_LOCK_NOTGRANTED:
123                 goto retry;
124         default:
125                 rc = LDAP_OTHER;
126                 text = "internal error";
127                 goto return_results;
128         }
129
130         if ( e == NULL ) {
131                 char* matched_dn = NULL;
132                 BerVarray refs;
133
134                 if( matched != NULL ) {
135                         matched_dn = ch_strdup( matched->e_dn );
136                         refs = is_entry_referral( matched )
137                                 ? get_entry_referrals( be, conn, op, matched )
138                                 : NULL;
139                         bdb_cache_return_entry_r( &bdb->bi_cache, matched );
140                         matched = NULL;
141
142                 } else {
143                         refs = referral_rewrite( default_referral,
144                                 NULL, dn, LDAP_SCOPE_DEFAULT );
145                 }
146
147                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
148                         matched_dn, NULL, refs, NULL );
149
150                 ber_bvarray_free( refs );
151                 free( matched_dn );
152
153                 goto done;
154         }
155
156         if (!manageDSAit && is_entry_referral( e ) ) {
157                 /* parent is a referral, don't allow add */
158                 /* parent is an alias, don't allow add */
159                 BerVarray refs = get_entry_referrals( be,
160                         conn, op, e );
161
162                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
163                         e->e_dn, 0, 0 );
164
165                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
166                         e->e_dn, NULL, refs, NULL );
167
168                 ber_bvarray_free( refs );
169                 goto done;
170         }
171
172         if ( be_issuffix( be, &e->e_nname ) ) {
173                 p_ndn = slap_empty_bv;
174         } else {
175                 dnParent( &e->e_nname, &p_ndn );
176         }
177         np_ndn = &p_ndn;
178         if ( p_ndn.bv_len != 0 ) {
179                 /* Make sure parent entry exist and we can write its 
180                  * children.
181                  */
182                 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0 );
183
184                 switch( rc ) {
185                 case 0:
186                 case DB_NOTFOUND:
187                         break;
188                 case DB_LOCK_DEADLOCK:
189                 case DB_LOCK_NOTGRANTED:
190                         goto retry;
191                 default:
192                         rc = LDAP_OTHER;
193                         text = "internal error";
194                         goto return_results;
195                 }
196
197                 if( p == NULL) {
198                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
199                                 0, 0, 0);
200                         rc = LDAP_OTHER;
201                         goto return_results;
202                 }
203
204                 /* check parent for "children" acl */
205                 if ( ! access_allowed( be, conn, op, p,
206                         children, NULL, ACL_WRITE, NULL ) )
207                 {
208                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
209                                 0, 0 );
210                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
211                                 NULL, NULL, NULL, NULL );
212                         goto return_results;
213                 }
214
215                 Debug( LDAP_DEBUG_TRACE,
216                         "bdb_modrdn: wr to children of entry %s OK\n",
217                         p_ndn.bv_val, 0, 0 );
218                 
219                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
220                         p_dn = slap_empty_bv;
221                 } else {
222                         dnParent( &e->e_name, &p_dn );
223                 }
224
225                 Debug( LDAP_DEBUG_TRACE,
226                         "bdb_modrdn: parent dn=%s\n",
227                         p_dn.bv_val, 0, 0 );
228
229         } else {
230                 /* no parent, modrdn entry directly under root */
231                 isroot = be_isroot( be, &op->o_ndn );
232                 if ( ! isroot ) {
233                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
234                                 || be_isupdate( be, &op->o_ndn ) ) {
235
236                                 p = (Entry *)&slap_entry_root;
237
238                                 /* check parent for "children" acl */
239                                 rc = access_allowed( be, conn, op, p,
240                                         children, NULL, ACL_WRITE, NULL );
241
242                                 p = NULL;
243
244                                 if ( ! rc )
245                                 {
246                                         Debug( LDAP_DEBUG_TRACE, 
247                                                 "no access to parent\n", 
248                                                 0, 0, 0 );
249                                         send_ldap_result( conn, op, 
250                                                 LDAP_INSUFFICIENT_ACCESS,
251                                                 NULL, NULL, NULL, NULL );
252                                         goto return_results;
253                                 }
254
255                                 Debug( LDAP_DEBUG_TRACE,
256                                         "bdb_modrdn: wr to children of entry \"\" OK\n",
257                                         0, 0, 0 );
258                 
259                                 p_dn.bv_val = "";
260                                 p_dn.bv_len = 0;
261
262                                 Debug( LDAP_DEBUG_TRACE,
263                                         "bdb_modrdn: parent dn=\"\"\n",
264                                         0, 0, 0 );
265
266                         } else {
267                                 Debug( LDAP_DEBUG_TRACE,
268                                         "bdb_modrdn: no parent, not root "
269                                         "& \"\" is not suffix\n",
270                                         0, 0, 0);
271                                 rc = LDAP_INSUFFICIENT_ACCESS;
272                                 goto return_results;
273                         }
274                 }
275         }
276
277         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
278
279         if ( newSuperior != NULL ) {
280                 Debug( LDAP_DEBUG_TRACE, 
281                         "bdb_modrdn: new parent \"%s\" requested...\n",
282                         newSuperior->bv_val, 0, 0 );
283
284                 if ( newSuperior->bv_len ) {
285                         np_dn = newSuperior;
286                         np_ndn = nnewSuperior;
287
288                         /* newSuperior == oldParent?, if so ==> ERROR */
289                         /* newSuperior == entry being moved?, if so ==> ERROR */
290                         /* Get Entry with dn=newSuperior. Does newSuperior exist? */
291
292                         rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0 );
293
294                         switch( rc ) {
295                         case 0:
296                         case DB_NOTFOUND:
297                                 break;
298                         case DB_LOCK_DEADLOCK:
299                         case DB_LOCK_NOTGRANTED:
300                                 goto retry;
301                         default:
302                                 rc = LDAP_OTHER;
303                                 text = "internal error";
304                                 goto return_results;
305                         }
306
307                         if( np == NULL) {
308                                 Debug( LDAP_DEBUG_TRACE,
309                                         "bdb_modrdn: newSup(ndn=%s) not here!\n",
310                                         np_ndn->bv_val, 0, 0);
311                                 rc = LDAP_OTHER;
312                                 goto return_results;
313                         }
314
315                         Debug( LDAP_DEBUG_TRACE,
316                                 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
317                                 np, (long) np->e_id, 0 );
318
319                         /* check newSuperior for "children" acl */
320                         if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
321                                 Debug( LDAP_DEBUG_TRACE,
322                                         "bdb_modrdn: no wr to newSup children\n",
323                                         0, 0, 0 );
324                                 rc = LDAP_INSUFFICIENT_ACCESS;
325                                 goto return_results;
326                         }
327
328 #ifdef BDB_ALIASES
329                         if ( is_entry_alias( np ) ) {
330                                 /* parent is an alias, don't allow add */
331                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
332                                         0, 0, 0 );
333
334                                 rc = LDAP_ALIAS_PROBLEM;
335                                 goto return_results;
336                         }
337 #endif
338
339                         if ( is_entry_referral( np ) ) {
340                                 /* parent is a referral, don't allow add */
341                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
342                                         0, 0, 0 );
343
344                                 rc = LDAP_OPERATIONS_ERROR;
345                                 goto return_results;
346                         }
347
348                 } else {
349                         if ( isroot == -1 ) {
350                                 isroot = be_isroot( be, &op->o_ndn );
351                         }
352                         
353                         np_dn = NULL;
354
355                         /* no parent, modrdn entry directly under root */
356                         if ( ! isroot ) {
357                                 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
358                                         || be_isupdate( be, &op->o_ndn ) ) {
359                                         np = (Entry *)&slap_entry_root;
360
361                                         /* check parent for "children" acl */
362                                         rc = access_allowed( be, conn, op, np,
363                                                 children, NULL, ACL_WRITE, NULL );
364
365                                         np = NULL;
366
367                                         if ( ! rc )
368                                         {
369                                                 Debug( LDAP_DEBUG_TRACE, 
370                                                         "no access to new superior\n", 
371                                                         0, 0, 0 );
372                                                 send_ldap_result( conn, op, 
373                                                         LDAP_INSUFFICIENT_ACCESS,
374                                                         NULL, NULL, NULL, NULL );
375                                                 goto return_results;
376                                         }
377
378                                         Debug( LDAP_DEBUG_TRACE,
379                                                 "bdb_modrdn: wr to children of entry \"\" OK\n",
380                                                 0, 0, 0 );
381                 
382                                 } else {
383                                         Debug( LDAP_DEBUG_TRACE,
384                                                 "bdb_modrdn: new superior=\"\", not root "
385                                                 "& \"\" is not suffix\n",
386                                                 0, 0, 0);
387                                         rc = LDAP_INSUFFICIENT_ACCESS;
388                                         goto return_results;
389                                 }
390                         }
391
392                         Debug( LDAP_DEBUG_TRACE,
393                                 "bdb_modrdn: new superior=\"\"\n",
394                                 0, 0, 0 );
395                 }
396
397                 Debug( LDAP_DEBUG_TRACE,
398                         "bdb_modrdn: wr to new parent's children OK\n",
399                         0, 0, 0 );
400
401                 new_parent_dn = np_dn;
402         }
403         
404         /* Build target dn and make sure target entry doesn't exist already. */
405         build_new_dn( &new_dn, new_parent_dn, newrdn ); 
406
407         dnNormalize2( NULL, &new_dn, &new_ndn );
408
409         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
410                 new_ndn.bv_val, 0, 0 );
411
412         rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
413         switch( rc ) {
414         case DB_LOCK_DEADLOCK:
415         case DB_LOCK_NOTGRANTED:
416                 goto retry;
417         case DB_NOTFOUND:
418                 break;
419         case 0:
420                 rc = LDAP_ALREADY_EXISTS;
421                 goto return_results;
422         default:
423                 rc = LDAP_OTHER;
424                 text = "internal error";
425                 goto return_results;
426         }
427
428         Debug( LDAP_DEBUG_TRACE,
429                 "bdb_modrdn: new ndn=%s does not exist\n",
430                 new_ndn.bv_val, 0, 0 );
431
432         /* Get attribute type and attribute value of our new rdn, we will
433          * need to add that to our new entry
434          */
435         if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
436                 LDAP_DN_FORMAT_LDAP ) )
437         {
438                 Debug( LDAP_DEBUG_TRACE,
439                         "bdb_modrdn: can't figure out type(s)/values(s) "
440                         "of newrdn\n", 0, 0, 0 );
441                 rc = LDAP_OPERATIONS_ERROR;
442                 text = "unknown type(s) used in RDN";
443                 goto return_results;            
444         }
445
446         Debug( LDAP_DEBUG_TRACE,
447                 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
448                 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
449
450         if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
451                 LDAP_DN_FORMAT_LDAP ) )
452         {
453                 Debug( LDAP_DEBUG_TRACE,
454                         "bdb_back_modrdn: can't figure out the old_rdn "
455                         "type(s)/value(s)\n", 0, 0, 0 );
456                 rc = LDAP_OTHER;
457                 text = "cannot parse RDN from old DN";
458                 goto return_results;            
459         }
460
461 #if 0
462         if ( newSuperior == NULL
463                 && charray_strcasecmp( ( const char ** )old_rdn_types, 
464                                 ( const char ** )new_rdn_types ) != 0 ) {
465                 /* Not a big deal but we may say something */
466                 Debug( LDAP_DEBUG_TRACE,
467                         "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
468                         "do not match\n", 
469                         old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
470         }               
471 #endif
472
473         /* Add new attribute values to the entry */
474         for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
475                 int                     rc;
476                 AttributeDescription    *desc = NULL;
477                 Modifications           *mod_tmp;
478
479                 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
480
481                 if ( rc != LDAP_SUCCESS ) {
482                         Debug( LDAP_DEBUG_TRACE,
483                                 "bdb_modrdn: %s: %s (new)\n",
484                                 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
485                         goto return_results;            
486                 }
487
488                 /* ACL check of newly added attrs */
489                 if ( !access_allowed( be, conn, op, e, desc,
490                         &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
491                         Debug( LDAP_DEBUG_TRACE,
492                                 "bdb_modrdn: access to attr \"%s\" "
493                                 "(new) not allowed\n", 
494                                 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
495                         rc = LDAP_INSUFFICIENT_ACCESS;
496                         goto return_results;
497                 }
498
499                 /* Apply modification */
500                 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
501                         + 2 * sizeof( struct berval ) );
502                 mod_tmp->sml_desc = desc;
503                 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
504                 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
505                 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
506                 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
507                 mod_tmp->sml_next = mod;
508                 mod = mod_tmp;
509         }
510
511         /* Remove old rdn value if required */
512         if ( deleteoldrdn ) {
513                 /* Get value of old rdn */
514                 if ( old_rdn == NULL) {
515                         Debug( LDAP_DEBUG_TRACE,
516                                 "bdb_modrdn: can't figure out old RDN value(s) "
517                                 "from old RDN\n", 0, 0, 0 );
518                         rc = LDAP_OTHER;
519                         text = "could not parse value(s) from old RDN";
520                         goto return_results;            
521                 }
522
523                 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
524                         int                     rc;
525                         AttributeDescription    *desc = NULL;
526                         Modifications           *mod_tmp;
527
528                         rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
529                                         &desc, &text );
530
531                         if ( rc != LDAP_SUCCESS ) {
532                                 Debug( LDAP_DEBUG_TRACE,
533                                         "bdb_modrdn: %s: %s (old)\n",
534                                         text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
535                                 goto return_results;            
536                         }
537
538                         /* ACL check of newly added attrs */
539                         if ( !access_allowed( be, conn, op, e, desc,
540                                 &old_rdn[0][d_cnt]->la_value, ACL_WRITE, NULL ) ) {
541                                 Debug( LDAP_DEBUG_TRACE,
542                                         "bdb_modrdn: access to attr \"%s\" "
543                                         "(old) not allowed\n", 
544                                         old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
545                                 rc = LDAP_INSUFFICIENT_ACCESS;
546                                 goto return_results;
547                         }
548
549                         /* Apply modification */
550                         mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
551                                 + 2 * sizeof ( struct berval ) );
552                         mod_tmp->sml_desc = desc;
553                         mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
554                         mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
555                         mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
556                         mod_tmp->sml_op = LDAP_MOD_DELETE;
557                         mod_tmp->sml_next = mod;
558                         mod = mod_tmp;
559                 }
560         }
561         
562         /* delete old one */
563         rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
564         if ( rc != 0 ) {
565                 switch( rc ) {
566                 case DB_LOCK_DEADLOCK:
567                 case DB_LOCK_NOTGRANTED:
568                         goto retry;
569                 }
570                 rc = LDAP_OTHER;
571                 text = "DN index delete fail";
572                 goto return_results;
573         }
574
575         (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
576
577         /* Binary format uses a single contiguous block, cannot
578          * free individual fields. Leave new_dn/new_ndn set so
579          * they can be individually freed later.
580          */
581         e->e_name = new_dn;
582         e->e_nname = new_ndn;
583
584         new_dn.bv_val = NULL;
585         new_ndn.bv_val = NULL;
586
587         /* add new one */
588         rc = bdb_dn2id_add( be, ltid, np_ndn, e );
589         if ( rc != 0 ) {
590                 switch( rc ) {
591                 case DB_LOCK_DEADLOCK:
592                 case DB_LOCK_NOTGRANTED:
593                         goto retry;
594                 }
595                 rc = LDAP_OTHER;
596                 text = "DN index add failed";
597                 goto return_results;
598         }
599
600         /* modify entry */
601         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
602                 &text, textbuf, textlen );
603
604         if( rc != LDAP_SUCCESS ) {
605                 switch( rc ) {
606                 case DB_LOCK_DEADLOCK:
607                 case DB_LOCK_NOTGRANTED:
608                         goto retry;
609                 }
610                 goto return_results;
611         }
612         
613         /* id2entry index */
614         rc = bdb_id2entry_update( be, ltid, e );
615         if ( rc != 0 ) {
616                 switch( rc ) {
617                 case DB_LOCK_DEADLOCK:
618                 case DB_LOCK_NOTGRANTED:
619                         goto retry;
620                 }
621                 rc = LDAP_OTHER;
622                 text = "entry update failed";
623                 goto return_results;
624         }
625
626         if( op->o_noop ) {
627                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
628                         text = "txn_abort (no-op) failed";
629                 } else {
630                         rc = LDAP_SUCCESS;
631                 }
632
633         } else {
634                 if(( rc=TXN_PREPARE( ltid, bdb_gid )) != 0 ) {
635                         text = "txn_prepare failed";
636                 } else {
637                         if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
638                                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
639                                         text ="cache update & txn_abort failed";
640                                 } else {
641                                         rc = LDAP_OTHER;
642                                         text = "cache update failed";
643                                 }
644
645                         } else {
646                                 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
647                                         text = "txn_commit failed";
648                                 } else {
649                                         rc = LDAP_SUCCESS;
650                                 }
651                         }
652                 }
653         }
654  
655         ltid = NULL;
656         op->o_private = NULL;
657  
658         if( rc == LDAP_SUCCESS ) {
659                 Debug(LDAP_DEBUG_TRACE,
660                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
661                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
662                 text = NULL;
663                 bdb_cache_entry_commit( e );
664
665         } else {
666                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
667                         text, db_strerror(rc), rc );
668                 rc = LDAP_OTHER;
669         }
670
671 return_results:
672         send_ldap_result( conn, op, rc,
673                 NULL, text, NULL, NULL );
674
675         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
676                 ldap_pvt_thread_yield();
677                 TXN_CHECKPOINT( bdb->bi_dbenv,
678                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
679         }
680
681 done:
682         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
683         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
684
685         /* LDAP v2 supporting correct attribute handling. */
686         if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
687         if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
688         if( mod != NULL ) {
689                 Modifications *tmp;
690                 for (; mod; mod=tmp ) {
691                         tmp = mod->sml_next;
692                         free( mod );
693                 }
694         }
695
696         /* LDAP v3 Support */
697         if( np != NULL ) {
698                 /* free new parent and reader lock */
699                 bdb_cache_return_entry_r(&bdb->bi_cache, np);
700         }
701
702         if( p != NULL ) {
703                 /* free parent and reader lock */
704                 bdb_cache_return_entry_r(&bdb->bi_cache, p);
705         }
706
707         /* free entry */
708         if( e != NULL ) {
709                 bdb_cache_return_entry_w( &bdb->bi_cache, e );
710         }
711
712         if( ltid != NULL ) {
713                 TXN_ABORT( ltid );
714                 op->o_private = NULL;
715         }
716
717         return rc;
718 }