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