]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
First round of imports from HEAD
[openldap] / servers / slapd / back-bdb / modrdn.c
1 /* modrdn.c - bdb backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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         AttributeDescription *entry = slap_schema.si_ad_entry;
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 = NULL;
36         Entry           *p = NULL;
37         Entry           *matched;
38         /* LDAP v2 supporting correct attribute handling. */
39         LDAPRDN         *new_rdn = NULL;
40         LDAPRDN         *old_rdn = NULL;
41         int             rc;
42         const char *text;
43         char textbuf[SLAP_TEXT_BUFLEN];
44         size_t textlen = sizeof textbuf;
45         DB_TXN *        ltid = NULL;
46         struct bdb_op_info opinfo;
47
48         ID                      id;
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         u_int32_t       locker = 0;
61         DB_LOCK         lock;
62
63         int             noop = 0;
64
65 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
66         Operation* ps_list;
67         struct psid_entry* pm_list;
68         struct psid_entry* pm_prev;
69 #endif
70
71 #ifdef NEW_LOGGING
72         LDAP_LOG ( OPERATION, ENTRY, "==>bdb_modrdn(%s,%s,%s)\n", 
73                 dn->bv_val,newrdn->bv_val, newSuperior ? newSuperior->bv_val : "NULL" );
74 #else
75         Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
76                 dn->bv_val, newrdn->bv_val,
77                 newSuperior ? newSuperior->bv_val : "NULL" );
78 #endif
79
80         if( 0 ) {
81 retry:  /* transaction retry */
82                 if (e != NULL) {
83                         bdb_cache_delete_entry(&bdb->bi_cache, e);
84                         bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
85                 }
86                 if (p != NULL) {
87                         bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
88                 }
89                 if (np != NULL) {
90                         bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
91                 }
92 #ifdef NEW_LOGGING
93                 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
94 #else
95                 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
96 #endif
97
98 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
99                 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
100                 while ( pm_list != NULL ) {
101                         LDAP_LIST_REMOVE ( pm_list, link );
102                         pm_prev = pm_list;
103                         pm_list = LDAP_LIST_NEXT ( pm_list, link );
104                         free (pm_prev);
105                 }
106 #endif
107
108                 rc = TXN_ABORT( ltid );
109                 ltid = NULL;
110                 op->o_private = NULL;
111                 if( rc != 0 ) {
112                         rc = LDAP_OTHER;
113                         text = "internal error";
114                         goto return_results;
115                 }
116                 ldap_pvt_thread_yield();
117         }
118
119         /* begin transaction */
120         rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
121                 bdb->bi_db_opflags );
122         text = NULL;
123         if( rc != 0 ) {
124 #ifdef NEW_LOGGING
125                 LDAP_LOG ( OPERATION, ERR, 
126                         "==>bdb_modrdn: txn_begin failed: %s (%d)\n", 
127                         db_strerror(rc), rc, 0 );
128 #else
129                 Debug( LDAP_DEBUG_TRACE,
130                         "bdb_delete: txn_begin failed: %s (%d)\n",
131                         db_strerror(rc), rc, 0 );
132 #endif
133                 rc = LDAP_OTHER;
134                 text = "internal error";
135                 goto return_results;
136         }
137
138         locker = TXN_ID ( ltid );
139
140         opinfo.boi_bdb = be;
141         opinfo.boi_txn = ltid;
142         opinfo.boi_locker = locker;
143         opinfo.boi_err = 0;
144         op->o_private = &opinfo;
145
146         /* get entry */
147         rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
148
149         switch( rc ) {
150         case 0:
151         case DB_NOTFOUND:
152                 break;
153         case DB_LOCK_DEADLOCK:
154         case DB_LOCK_NOTGRANTED:
155                 goto retry;
156         case LDAP_BUSY:
157                 text = "ldap server busy";
158                 goto return_results;
159         default:
160                 rc = LDAP_OTHER;
161                 text = "internal error";
162                 goto return_results;
163         }
164
165         if ( e == NULL ) {
166                 char* matched_dn = NULL;
167                 BerVarray refs;
168
169                 if( matched != NULL ) {
170                         matched_dn = ch_strdup( matched->e_dn );
171                         refs = is_entry_referral( matched )
172                                 ? get_entry_referrals( be, conn, op, matched )
173                                 : NULL;
174                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
175                         matched = NULL;
176
177                 } else {
178                         refs = referral_rewrite( default_referral,
179                                 NULL, dn, LDAP_SCOPE_DEFAULT );
180                 }
181
182                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
183                         matched_dn, NULL, refs, NULL );
184
185                 ber_bvarray_free( refs );
186                 free( matched_dn );
187
188                 goto done;
189         }
190
191         /* check write on old entry */
192         rc = access_allowed( be, conn, op, e, entry, NULL, ACL_WRITE, NULL );
193
194         switch( opinfo.boi_err ) {
195         case DB_LOCK_DEADLOCK:
196         case DB_LOCK_NOTGRANTED:
197                 goto retry;
198         }
199
200         if ( ! rc ) {
201 #ifdef NEW_LOGGING
202                 LDAP_LOG ( OPERATION, ERR, 
203                         "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
204 #else
205                 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
206                         0, 0 );
207 #endif
208                 text = "no write access to old entry";
209                 rc = LDAP_INSUFFICIENT_ACCESS;
210                 goto return_results;
211         }
212
213         if (!manageDSAit && is_entry_referral( e ) ) {
214                 /* parent is a referral, don't allow add */
215                 /* parent is an alias, don't allow add */
216                 BerVarray refs = get_entry_referrals( be,
217                         conn, op, e );
218
219 #ifdef NEW_LOGGING
220                 LDAP_LOG ( OPERATION, DETAIL1, 
221                         "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
222 #else
223                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
224                         e->e_dn, 0, 0 );
225 #endif
226
227                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
228                         e->e_dn, NULL, refs, NULL );
229
230                 ber_bvarray_free( refs );
231                 goto done;
232         }
233
234         if ( be_issuffix( be, &e->e_nname ) ) {
235                 p_ndn = slap_empty_bv;
236         } else {
237                 dnParent( &e->e_nname, &p_ndn );
238         }
239         np_ndn = &p_ndn;
240         if ( p_ndn.bv_len != 0 ) {
241                 /* Make sure parent entry exist and we can write its 
242                  * children.
243                  */
244                 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
245
246                 switch( rc ) {
247                 case 0:
248                 case DB_NOTFOUND:
249                         break;
250                 case DB_LOCK_DEADLOCK:
251                 case DB_LOCK_NOTGRANTED:
252                         goto retry;
253                 case LDAP_BUSY:
254                         text = "ldap server busy";
255                         goto return_results;
256                 default:
257                         rc = LDAP_OTHER;
258                         text = "internal error";
259                         goto return_results;
260                 }
261
262                 if( p == NULL) {
263 #ifdef NEW_LOGGING
264                         LDAP_LOG ( OPERATION, ERR, 
265                                 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
266 #else
267                         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
268                                 0, 0, 0);
269 #endif
270                         rc = LDAP_OTHER;
271                         text = "old entry's parent does not exist";
272                         goto return_results;
273                 }
274
275                 /* check parent for "children" acl */
276                 rc = access_allowed( be, conn, op, p,
277                         children, NULL, ACL_WRITE, NULL );
278
279                 if ( ! rc ) {
280                         rc = LDAP_INSUFFICIENT_ACCESS;
281 #ifdef NEW_LOGGING
282                         LDAP_LOG ( OPERATION, ERR, 
283                                 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
284 #else
285                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
286                                 0, 0 );
287 #endif
288                         text = "no write access to old parent's children";
289                         goto return_results;
290                 }
291
292 #ifdef NEW_LOGGING
293                 LDAP_LOG ( OPERATION, DETAIL1, 
294                         "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
295 #else
296                 Debug( LDAP_DEBUG_TRACE,
297                         "bdb_modrdn: wr to children of entry %s OK\n",
298                         p_ndn.bv_val, 0, 0 );
299 #endif
300                 
301                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
302                         p_dn = slap_empty_bv;
303                 } else {
304                         dnParent( &e->e_name, &p_dn );
305                 }
306
307 #ifdef NEW_LOGGING
308                 LDAP_LOG ( OPERATION, DETAIL1, 
309                         "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
310 #else
311                 Debug( LDAP_DEBUG_TRACE,
312                         "bdb_modrdn: parent dn=%s\n",
313                         p_dn.bv_val, 0, 0 );
314 #endif
315
316         } else {
317                 /* no parent, modrdn entry directly under root */
318                 isroot = be_isroot( be, &op->o_ndn );
319                 if ( ! isroot ) {
320                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
321                                 || be_isupdate( be, &op->o_ndn ) ) {
322
323                                 p = (Entry *)&slap_entry_root;
324
325                                 /* check parent for "children" acl */
326                                 rc = access_allowed( be, conn, op, p,
327                                         children, NULL, ACL_WRITE, NULL );
328
329                                 p = NULL;
330
331                                 if ( ! rc ) {
332                                         rc = LDAP_INSUFFICIENT_ACCESS;
333 #ifdef NEW_LOGGING
334                                         LDAP_LOG ( OPERATION, ERR, 
335                                                 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
336 #else
337                                         Debug( LDAP_DEBUG_TRACE, 
338                                                 "no access to parent\n", 
339                                                 0, 0, 0 );
340 #endif
341                                         text = "no write access to old parent";
342                                         goto return_results;
343                                 }
344
345 #ifdef NEW_LOGGING
346                                 LDAP_LOG ( OPERATION, DETAIL1, 
347                                         "==>bdb_modrdn: wr to children of entry \"%s\" OK\n", 
348                                         p_dn.bv_val, 0, 0 );
349 #else
350                                 Debug( LDAP_DEBUG_TRACE,
351                                         "bdb_modrdn: wr to children of entry \"\" OK\n",
352                                         0, 0, 0 );
353 #endif
354                 
355                                 p_dn.bv_val = "";
356                                 p_dn.bv_len = 0;
357
358 #ifdef NEW_LOGGING
359                                 LDAP_LOG ( OPERATION, DETAIL1, 
360                                         "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
361 #else
362                                 Debug( LDAP_DEBUG_TRACE,
363                                         "bdb_modrdn: parent dn=\"\"\n",
364                                         0, 0, 0 );
365 #endif
366
367                         } else {
368 #ifdef NEW_LOGGING
369                                 LDAP_LOG ( OPERATION, ERR, 
370                                         "==>bdb_modrdn: no parent, not root &\"\" is not "
371                                         "suffix\n", 0, 0, 0 );
372 #else
373                                 Debug( LDAP_DEBUG_TRACE,
374                                         "bdb_modrdn: no parent, not root "
375                                         "& \"\" is not suffix\n",
376                                         0, 0, 0);
377 #endif
378                                 text = "no write access to old parent";
379                                 rc = LDAP_INSUFFICIENT_ACCESS;
380                                 goto return_results;
381                         }
382                 }
383         }
384
385         new_parent_dn = &p_dn;  /* New Parent unless newSuperior given */
386
387         if ( newSuperior != NULL ) {
388 #ifdef NEW_LOGGING
389                 LDAP_LOG ( OPERATION, DETAIL1, 
390                         "==>bdb_modrdn: new parent \"%s\" requested...\n", 
391                         newSuperior->bv_val, 0, 0 );
392 #else
393                 Debug( LDAP_DEBUG_TRACE, 
394                         "bdb_modrdn: new parent \"%s\" requested...\n",
395                         newSuperior->bv_val, 0, 0 );
396 #endif
397
398                 /*  newSuperior == oldParent? */
399                 if( dn_match( &p_ndn, nnewSuperior ) ) {
400 #ifdef NEW_LOGGING
401                         LDAP_LOG( BACK_LDBM, INFO, "bdb_back_modrdn: "
402                                 "new parent \"%s\" same as the old parent \"%s\"\n",
403                                 newSuperior->bv_val, p_dn.bv_val, 0 );
404 #else
405                         Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
406                                 "new parent \"%s\" same as the old parent \"%s\"\n",
407                                 newSuperior->bv_val, p_dn.bv_val, 0 );
408 #endif      
409                         newSuperior = NULL; /* ignore newSuperior */
410                 }
411         }
412
413         if ( newSuperior != NULL ) {
414                 if ( newSuperior->bv_len ) {
415                         np_dn = newSuperior;
416                         np_ndn = nnewSuperior;
417
418                         /* newSuperior == oldParent?, if so ==> ERROR */
419                         /* newSuperior == entry being moved?, if so ==> ERROR */
420                         /* Get Entry with dn=newSuperior. Does newSuperior exist? */
421
422                         rc = bdb_dn2entry_r( be,
423                                 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
424
425                         switch( rc ) {
426                         case 0:
427                         case DB_NOTFOUND:
428                                 break;
429                         case DB_LOCK_DEADLOCK:
430                         case DB_LOCK_NOTGRANTED:
431                                 goto retry;
432                         case LDAP_BUSY:
433                                 text = "ldap server busy";
434                                 goto return_results;
435                         default:
436                                 rc = LDAP_OTHER;
437                                 text = "internal error";
438                                 goto return_results;
439                         }
440
441                         if( np == NULL) {
442 #ifdef NEW_LOGGING
443                                 LDAP_LOG ( OPERATION, DETAIL1, 
444                                         "==>bdb_modrdn: newSup(ndn=%s) not here!\n", 
445                                         np_ndn->bv_val, 0, 0 );
446 #else
447                                 Debug( LDAP_DEBUG_TRACE,
448                                         "bdb_modrdn: newSup(ndn=%s) not here!\n",
449                                         np_ndn->bv_val, 0, 0);
450 #endif
451                                 text = "new superior not found";
452                                 rc = LDAP_OTHER;
453                                 goto return_results;
454                         }
455
456 #ifdef NEW_LOGGING
457                         LDAP_LOG ( OPERATION, DETAIL1, 
458                                 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n", 
459                                 np, (long) np->e_id, 0 );
460 #else
461                         Debug( LDAP_DEBUG_TRACE,
462                                 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
463                                 np, (long) np->e_id, 0 );
464 #endif
465
466                         /* check newSuperior for "children" acl */
467                         rc = access_allowed( be, conn, op, np, children,
468                                 NULL, ACL_WRITE, NULL );
469
470                         if( ! rc ) {
471 #ifdef NEW_LOGGING
472                                 LDAP_LOG ( OPERATION, DETAIL1, 
473                                         "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
474 #else
475                                 Debug( LDAP_DEBUG_TRACE,
476                                         "bdb_modrdn: no wr to newSup children\n",
477                                         0, 0, 0 );
478 #endif
479                                 text = "no write access to new superior's children";
480                                 rc = LDAP_INSUFFICIENT_ACCESS;
481                                 goto return_results;
482                         }
483
484 #ifdef BDB_ALIASES
485                         if ( is_entry_alias( np ) ) {
486                                 /* parent is an alias, don't allow add */
487 #ifdef NEW_LOGGING
488                                 LDAP_LOG ( OPERATION, DETAIL1, 
489                                         "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
490 #else
491                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
492                                         0, 0, 0 );
493 #endif
494                                 text = "new superior is an alias";
495                                 rc = LDAP_ALIAS_PROBLEM;
496                                 goto return_results;
497                         }
498 #endif
499
500                         if ( is_entry_referral( np ) ) {
501                                 /* parent is a referral, don't allow add */
502 #ifdef NEW_LOGGING
503                                 LDAP_LOG ( OPERATION, DETAIL1, 
504                                         "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
505 #else
506                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
507                                         0, 0, 0 );
508 #endif
509                                 text = "new superior is a referral";
510                                 rc = LDAP_OTHER;
511                                 goto return_results;
512                         }
513
514                 } else {
515                         if ( isroot == -1 ) {
516                                 isroot = be_isroot( be, &op->o_ndn );
517                         }
518                         
519                         np_dn = NULL;
520
521                         /* no parent, modrdn entry directly under root */
522                         if ( ! isroot ) {
523                                 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
524                                         || be_isupdate( be, &op->o_ndn ) ) {
525                                         np = (Entry *)&slap_entry_root;
526
527                                         /* check parent for "children" acl */
528                                         rc = access_allowed( be, conn, op, np,
529                                                 children, NULL, ACL_WRITE, NULL );
530
531                                         np = NULL;
532
533                                         if ( ! rc ) {
534                                                 rc = LDAP_INSUFFICIENT_ACCESS;
535 #ifdef NEW_LOGGING
536                                                 LDAP_LOG ( OPERATION, ERR, 
537                                                         "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
538 #else
539                                                 Debug( LDAP_DEBUG_TRACE, 
540                                                         "no access to new superior\n", 
541                                                         0, 0, 0 );
542 #endif
543                                                 text = "no write access to new superior's children";
544                                                 goto return_results;
545                                         }
546
547 #ifdef NEW_LOGGING
548                                         LDAP_LOG ( OPERATION, DETAIL1, 
549                                                 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
550 #else
551                                         Debug( LDAP_DEBUG_TRACE,
552                                                 "bdb_modrdn: wr to children of entry \"\" OK\n",
553                                                 0, 0, 0 );
554 #endif
555                 
556                                 } else {
557 #ifdef NEW_LOGGING
558                                         LDAP_LOG ( OPERATION, ERR, 
559                                                 "bdb_modrdn: new superior=\"\", not root & \"\" "
560                                                 "is not suffix\n", 0, 0, 0 );
561 #else
562                                         Debug( LDAP_DEBUG_TRACE,
563                                                 "bdb_modrdn: new superior=\"\", not root "
564                                                 "& \"\" is not suffix\n",
565                                                 0, 0, 0);
566 #endif
567                                         text = "no write access to new superior's children";
568                                         rc = LDAP_INSUFFICIENT_ACCESS;
569                                         goto return_results;
570                                 }
571                         }
572
573 #ifdef NEW_LOGGING
574                         LDAP_LOG ( OPERATION, DETAIL1, 
575                                 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
576 #else
577                         Debug( LDAP_DEBUG_TRACE,
578                                 "bdb_modrdn: new superior=\"\"\n",
579                                 0, 0, 0 );
580 #endif
581                 }
582
583 #ifdef NEW_LOGGING
584                 LDAP_LOG ( OPERATION, DETAIL1, 
585                         "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
586 #else
587                 Debug( LDAP_DEBUG_TRACE,
588                         "bdb_modrdn: wr to new parent's children OK\n",
589                         0, 0, 0 );
590 #endif
591
592                 new_parent_dn = np_dn;
593         }
594
595         /* Build target dn and make sure target entry doesn't exist already. */
596         build_new_dn( &new_dn, new_parent_dn, newrdn ); 
597
598         dnNormalize2( NULL, &new_dn, &new_ndn );
599
600 #ifdef NEW_LOGGING
601         LDAP_LOG ( OPERATION, RESULTS, 
602                 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
603 #else
604         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
605                 new_ndn.bv_val, 0, 0 );
606 #endif
607
608         rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
609         switch( rc ) {
610         case DB_LOCK_DEADLOCK:
611         case DB_LOCK_NOTGRANTED:
612                 goto retry;
613         case DB_NOTFOUND:
614                 break;
615         case 0:
616                 rc = LDAP_ALREADY_EXISTS;
617                 goto return_results;
618         default:
619                 rc = LDAP_OTHER;
620                 text = "internal error";
621                 goto return_results;
622         }
623
624 #ifdef NEW_LOGGING
625         LDAP_LOG ( OPERATION, ERR, 
626                 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
627 #else
628         Debug( LDAP_DEBUG_TRACE,
629                 "bdb_modrdn: new ndn=%s does not exist\n",
630                 new_ndn.bv_val, 0, 0 );
631 #endif
632
633         /* Get attribute type and attribute value of our new rdn, we will
634          * need to add that to our new entry
635          */
636         if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
637                 LDAP_DN_FORMAT_LDAP ) )
638         {
639 #ifdef NEW_LOGGING
640                 LDAP_LOG ( OPERATION, ERR, 
641                         "bdb_modrdn: can't figure out "
642                         "type(s)/values(s) of newrdn\n", 
643                         0, 0, 0 );
644 #else
645                 Debug( LDAP_DEBUG_TRACE,
646                         "bdb_modrdn: can't figure out "
647                         "type(s)/values(s) of newrdn\n", 
648                         0, 0, 0 );
649 #endif
650                 rc = LDAP_INVALID_DN_SYNTAX;
651                 text = "unknown type(s) used in RDN";
652                 goto return_results;
653         }
654
655 #ifdef NEW_LOGGING
656         LDAP_LOG ( OPERATION, RESULTS, 
657                 "bdb_modrdn: new_rdn_type=\"%s\", "
658                 "new_rdn_val=\"%s\"\n",
659                 new_rdn[ 0 ][ 0 ]->la_attr.bv_val, 
660                 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
661 #else
662         Debug( LDAP_DEBUG_TRACE,
663                 "bdb_modrdn: new_rdn_type=\"%s\", "
664                 "new_rdn_val=\"%s\"\n",
665                 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
666                 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
667 #endif
668
669         if ( deleteoldrdn ) {
670                 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
671                         LDAP_DN_FORMAT_LDAP ) )
672                 {
673 #ifdef NEW_LOGGING
674                         LDAP_LOG ( OPERATION, ERR, 
675                                 "bdb_modrdn: can't figure out "
676                                 "type(s)/values(s) of old_rdn\n", 
677                                 0, 0, 0 );
678 #else
679                         Debug( LDAP_DEBUG_TRACE,
680                                 "bdb_modrdn: can't figure out "
681                                 "the old_rdn type(s)/value(s)\n", 
682                                 0, 0, 0 );
683 #endif
684                         rc = LDAP_OTHER;
685                         text = "cannot parse RDN from old DN";
686                         goto return_results;            
687                 }
688         }
689
690         /* prepare modlist of modifications from old/new rdn */
691         rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn, 
692                         deleteoldrdn, &mod );
693         if ( rc != LDAP_SUCCESS ) {
694                 goto return_results;
695         }
696         
697         /* delete old one */
698         rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
699         if ( rc != 0 ) {
700                 switch( rc ) {
701                 case DB_LOCK_DEADLOCK:
702                 case DB_LOCK_NOTGRANTED:
703                         goto retry;
704                 }
705                 rc = LDAP_OTHER;
706                 text = "DN index delete fail";
707                 goto return_results;
708         }
709
710         (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
711
712         /* Binary format uses a single contiguous block, cannot
713          * free individual fields. Leave new_dn/new_ndn set so
714          * they can be individually freed later.
715          */
716         e->e_name = new_dn;
717         e->e_nname = new_ndn;
718
719         new_dn.bv_val = NULL;
720         new_ndn.bv_val = NULL;
721
722         /* add new one */
723         rc = bdb_dn2id_add( be, ltid, np_ndn, e );
724         if ( rc != 0 ) {
725                 switch( rc ) {
726                 case DB_LOCK_DEADLOCK:
727                 case DB_LOCK_NOTGRANTED:
728                         goto retry;
729                 }
730                 rc = LDAP_OTHER;
731                 text = "DN index add failed";
732                 goto return_results;
733         }
734
735 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
736         if ( rc == LDAP_SUCCESS && !op->o_noop ) {
737                 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
738                         bdb_psearch(be, conn, op, ps_list, e, LDAP_PSEARCH_BY_PREMODIFY );
739                 }
740         }
741 #endif
742
743         /* modify entry */
744         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
745                 &text, textbuf, textlen );
746
747         if( rc != LDAP_SUCCESS ) {
748                 switch( rc ) {
749                 case DB_LOCK_DEADLOCK:
750                 case DB_LOCK_NOTGRANTED:
751                         goto retry;
752                 }
753                 goto return_results;
754         }
755         
756         /* id2entry index */
757         rc = bdb_id2entry_update( be, ltid, e );
758         if ( rc != 0 ) {
759                 switch( rc ) {
760                 case DB_LOCK_DEADLOCK:
761                 case DB_LOCK_NOTGRANTED:
762                         goto retry;
763                 }
764                 rc = LDAP_OTHER;
765                 text = "entry update failed";
766                 goto return_results;
767         }
768
769         if( op->o_noop ) {
770                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
771                         text = "txn_abort (no-op) failed";
772                 } else {
773                         noop = 1;
774                         rc = LDAP_SUCCESS;
775                 }
776
777         } else {
778                 char gid[DB_XIDDATASIZE];
779
780                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
781                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
782
783                 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
784                         text = "txn_prepare failed";
785                 } else {
786                         if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
787                                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
788                                         text ="cache update & txn_abort failed";
789                                 } else {
790                                         rc = LDAP_OTHER;
791                                         text = "cache update failed";
792                                 }
793
794                         } else {
795                                 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
796                                         text = "txn_commit failed";
797                                 } else {
798                                         rc = LDAP_SUCCESS;
799                                 }
800                         }
801                 }
802         }
803  
804         ltid = NULL;
805         op->o_private = NULL;
806  
807         if( rc == LDAP_SUCCESS ) {
808 #ifdef NEW_LOGGING
809                 LDAP_LOG ( OPERATION, RESULTS, 
810                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n", 
811                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
812 #else
813                 Debug(LDAP_DEBUG_TRACE,
814                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
815                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
816 #endif
817                 text = NULL;
818                 if ( !noop ) {
819                         bdb_cache_entry_commit( e );
820                 }
821
822         } else {
823 #ifdef NEW_LOGGING
824                 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n", 
825                         text, db_strerror(rc), rc );
826 #else
827                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
828                         text, db_strerror(rc), rc );
829 #endif
830                 rc = LDAP_OTHER;
831         }
832
833 return_results:
834         send_ldap_result( conn, op, rc,
835                 NULL, text, NULL, NULL );
836
837 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
838         if ( rc == LDAP_SUCCESS && !op->o_noop ) {
839                 /* Loop through in-scope entries for each psearch spec */
840                 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
841                         bdb_psearch( be, conn, op, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
842                 }
843                 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
844                 while ( pm_list != NULL ) {
845                         bdb_psearch(be, conn, op, pm_list->ps->op,
846                                                 e, LDAP_PSEARCH_BY_SCOPEOUT);
847                         LDAP_LIST_REMOVE ( pm_list, link );
848                         pm_prev = pm_list;
849                         pm_list = LDAP_LIST_NEXT ( pm_list, link );
850                         free (pm_prev);
851                 }
852         }
853 #endif
854
855         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
856                 ldap_pvt_thread_yield();
857                 TXN_CHECKPOINT( bdb->bi_dbenv,
858                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
859         }
860
861 done:
862         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
863         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
864
865         /* LDAP v2 supporting correct attribute handling. */
866         if ( new_rdn != NULL ) {
867                 ldap_rdnfree( new_rdn );
868         }
869         if ( old_rdn != NULL ) {
870                 ldap_rdnfree( old_rdn );
871         }
872         if( mod != NULL ) {
873                 Modifications *tmp;
874                 for (; mod; mod=tmp ) {
875                         tmp = mod->sml_next;
876                         free( mod );
877                 }
878         }
879
880         /* LDAP v3 Support */
881         if( np != NULL ) {
882                 /* free new parent and reader lock */
883                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
884         }
885
886         if( p != NULL ) {
887                 /* free parent and reader lock */
888                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
889         }
890
891         /* free entry */
892         if( e != NULL ) {
893                 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
894         }
895
896         if( ltid != NULL ) {
897 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
898                 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
899                 while ( pm_list != NULL ) {
900                         LDAP_LIST_REMOVE ( pm_list, link );
901                         pm_prev = pm_list;
902                         pm_list = LDAP_LIST_NEXT ( pm_list, link );
903                         free (pm_prev);
904                 }
905 #endif
906                 TXN_ABORT( ltid );
907                 op->o_private = NULL;
908         }
909
910         return ( ( rc == LDAP_SUCCESS ) ? noop : rc );
911 }