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