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