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