]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
bc7f6dc0cc61addb53502f2e2321738f7275bbbc
[openldap] / servers / slapd / back-bdb / modrdn.c
1 /* modrdn.c - bdb backend modrdn routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_modrdn(
18         Backend *be,
19         Connection      *conn,
20         Operation       *op,
21         struct berval   *dn,
22         struct berval   *ndn,
23         struct berval   *newrdn,
24         struct berval   *nnewrdn,
25         int             deleteoldrdn,
26         struct berval   *newSuperior,
27         struct berval   *nnewSuperior )
28 {
29         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30         AttributeDescription *children = slap_schema.si_ad_children;
31         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 #ifdef LDAP_CLIENT_UPDATE
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 #ifdef LDAP_CLIENT_UPDATE
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                 if ( ! access_allowed( be, conn, op, p,
276                         children, NULL, ACL_WRITE, NULL ) )
277                 {
278                         rc = LDAP_INSUFFICIENT_ACCESS;
279 #ifdef NEW_LOGGING
280                         LDAP_LOG ( OPERATION, ERR, 
281                                 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
282 #else
283                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
284                                 0, 0 );
285 #endif
286                         text = "no write access to old parent's children";
287                         goto return_results;
288                 }
289
290 #ifdef NEW_LOGGING
291                 LDAP_LOG ( OPERATION, DETAIL1, 
292                         "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
293 #else
294                 Debug( LDAP_DEBUG_TRACE,
295                         "bdb_modrdn: wr to children of entry %s OK\n",
296                         p_ndn.bv_val, 0, 0 );
297 #endif
298                 
299                 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
300                         p_dn = slap_empty_bv;
301                 } else {
302                         dnParent( &e->e_name, &p_dn );
303                 }
304
305 #ifdef NEW_LOGGING
306                 LDAP_LOG ( OPERATION, DETAIL1, 
307                         "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
308 #else
309                 Debug( LDAP_DEBUG_TRACE,
310                         "bdb_modrdn: parent dn=%s\n",
311                         p_dn.bv_val, 0, 0 );
312 #endif
313
314         } else {
315                 /* no parent, modrdn entry directly under root */
316                 isroot = be_isroot( be, &op->o_ndn );
317                 if ( ! isroot ) {
318                         if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
319                                 || be_isupdate( be, &op->o_ndn ) ) {
320
321                                 p = (Entry *)&slap_entry_root;
322
323                                 /* check parent for "children" acl */
324                                 rc = access_allowed( be, conn, op, p,
325                                         children, NULL, ACL_WRITE, NULL );
326
327                                 p = NULL;
328
329                                 if ( ! rc )
330                                 {
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         if ( newSuperior != NULL ) {
412                 if ( newSuperior->bv_len ) {
413                         np_dn = newSuperior;
414                         np_ndn = nnewSuperior;
415
416                         /* newSuperior == oldParent?, if so ==> ERROR */
417                         /* newSuperior == entry being moved?, if so ==> ERROR */
418                         /* Get Entry with dn=newSuperior. Does newSuperior exist? */
419
420                         rc = bdb_dn2entry_r( be,
421                                 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
422
423                         switch( rc ) {
424                         case 0:
425                         case DB_NOTFOUND:
426                                 break;
427                         case DB_LOCK_DEADLOCK:
428                         case DB_LOCK_NOTGRANTED:
429                                 goto retry;
430                         case LDAP_BUSY:
431                                 text = "ldap server busy";
432                                 goto return_results;
433                         default:
434                                 rc = LDAP_OTHER;
435                                 text = "internal error";
436                                 goto return_results;
437                         }
438
439                         if( np == NULL) {
440 #ifdef NEW_LOGGING
441                                 LDAP_LOG ( OPERATION, DETAIL1, 
442                                         "==>bdb_modrdn: newSup(ndn=%s) not here!\n", 
443                                         np_ndn->bv_val, 0, 0 );
444 #else
445                                 Debug( LDAP_DEBUG_TRACE,
446                                         "bdb_modrdn: newSup(ndn=%s) not here!\n",
447                                         np_ndn->bv_val, 0, 0);
448 #endif
449                                 text = "new superior not found";
450                                 rc = LDAP_OTHER;
451                                 goto return_results;
452                         }
453
454 #ifdef NEW_LOGGING
455                         LDAP_LOG ( OPERATION, DETAIL1, 
456                                 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n", 
457                                 np, (long) np->e_id, 0 );
458 #else
459                         Debug( LDAP_DEBUG_TRACE,
460                                 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
461                                 np, (long) np->e_id, 0 );
462 #endif
463
464                         /* check newSuperior for "children" acl */
465                         if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
466 #ifdef NEW_LOGGING
467                                 LDAP_LOG ( OPERATION, DETAIL1, 
468                                         "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
469 #else
470                                 Debug( LDAP_DEBUG_TRACE,
471                                         "bdb_modrdn: no wr to newSup children\n",
472                                         0, 0, 0 );
473 #endif
474                                 text = "no write access to new superior's children";
475                                 rc = LDAP_INSUFFICIENT_ACCESS;
476                                 goto return_results;
477                         }
478
479 #ifdef BDB_ALIASES
480                         if ( is_entry_alias( np ) ) {
481                                 /* parent is an alias, don't allow add */
482 #ifdef NEW_LOGGING
483                                 LDAP_LOG ( OPERATION, DETAIL1, 
484                                         "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
485 #else
486                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
487                                         0, 0, 0 );
488 #endif
489                                 text = "new superior is an alias";
490                                 rc = LDAP_ALIAS_PROBLEM;
491                                 goto return_results;
492                         }
493 #endif
494
495                         if ( is_entry_referral( np ) ) {
496                                 /* parent is a referral, don't allow add */
497 #ifdef NEW_LOGGING
498                                 LDAP_LOG ( OPERATION, DETAIL1, 
499                                         "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
500 #else
501                                 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
502                                         0, 0, 0 );
503 #endif
504                                 text = "new superior is a referral";
505                                 rc = LDAP_OTHER;
506                                 goto return_results;
507                         }
508
509                 } else {
510                         if ( isroot == -1 ) {
511                                 isroot = be_isroot( be, &op->o_ndn );
512                         }
513                         
514                         np_dn = NULL;
515
516                         /* no parent, modrdn entry directly under root */
517                         if ( ! isroot ) {
518                                 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
519                                         || be_isupdate( be, &op->o_ndn ) ) {
520                                         np = (Entry *)&slap_entry_root;
521
522                                         /* check parent for "children" acl */
523                                         rc = access_allowed( be, conn, op, np,
524                                                 children, NULL, ACL_WRITE, NULL );
525
526                                         np = NULL;
527
528                                         if ( ! rc )
529                                         {
530                                                 rc = LDAP_INSUFFICIENT_ACCESS;
531 #ifdef NEW_LOGGING
532                                                 LDAP_LOG ( OPERATION, ERR, 
533                                                         "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
534 #else
535                                                 Debug( LDAP_DEBUG_TRACE, 
536                                                         "no access to new superior\n", 
537                                                         0, 0, 0 );
538 #endif
539                                                 text = "no write access to new superior's children";
540                                                 goto return_results;
541                                         }
542
543 #ifdef NEW_LOGGING
544                                         LDAP_LOG ( OPERATION, DETAIL1, 
545                                                 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
546 #else
547                                         Debug( LDAP_DEBUG_TRACE,
548                                                 "bdb_modrdn: wr to children of entry \"\" OK\n",
549                                                 0, 0, 0 );
550 #endif
551                 
552                                 } else {
553 #ifdef NEW_LOGGING
554                                         LDAP_LOG ( OPERATION, ERR, 
555                                                 "bdb_modrdn: new superior=\"\", not root & \"\" "
556                                                 "is not suffix\n", 0, 0, 0 );
557 #else
558                                         Debug( LDAP_DEBUG_TRACE,
559                                                 "bdb_modrdn: new superior=\"\", not root "
560                                                 "& \"\" is not suffix\n",
561                                                 0, 0, 0);
562 #endif
563                                         text = "no write access to new superior's children";
564                                         rc = LDAP_INSUFFICIENT_ACCESS;
565                                         goto return_results;
566                                 }
567                         }
568
569 #ifdef NEW_LOGGING
570                         LDAP_LOG ( OPERATION, DETAIL1, 
571                                 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
572 #else
573                         Debug( LDAP_DEBUG_TRACE,
574                                 "bdb_modrdn: new superior=\"\"\n",
575                                 0, 0, 0 );
576 #endif
577                 }
578
579 #ifdef NEW_LOGGING
580                 LDAP_LOG ( OPERATION, DETAIL1, 
581                         "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
582 #else
583                 Debug( LDAP_DEBUG_TRACE,
584                         "bdb_modrdn: wr to new parent's children OK\n",
585                         0, 0, 0 );
586 #endif
587
588                 new_parent_dn = np_dn;
589         }
590
591         /* Build target dn and make sure target entry doesn't exist already. */
592         build_new_dn( &new_dn, new_parent_dn, newrdn ); 
593
594         dnNormalize2( NULL, &new_dn, &new_ndn );
595
596 #ifdef NEW_LOGGING
597         LDAP_LOG ( OPERATION, RESULTS, 
598                 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
599 #else
600         Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
601                 new_ndn.bv_val, 0, 0 );
602 #endif
603
604         rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
605         switch( rc ) {
606         case DB_LOCK_DEADLOCK:
607         case DB_LOCK_NOTGRANTED:
608                 goto retry;
609         case DB_NOTFOUND:
610                 break;
611         case 0:
612                 rc = LDAP_ALREADY_EXISTS;
613                 goto return_results;
614         default:
615                 rc = LDAP_OTHER;
616                 text = "internal error";
617                 goto return_results;
618         }
619
620 #ifdef NEW_LOGGING
621         LDAP_LOG ( OPERATION, ERR, 
622                 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
623 #else
624         Debug( LDAP_DEBUG_TRACE,
625                 "bdb_modrdn: new ndn=%s does not exist\n",
626                 new_ndn.bv_val, 0, 0 );
627 #endif
628
629         /* Get attribute type and attribute value of our new rdn, we will
630          * need to add that to our new entry
631          */
632         if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
633                 LDAP_DN_FORMAT_LDAP ) )
634         {
635 #ifdef NEW_LOGGING
636                 LDAP_LOG ( OPERATION, ERR, 
637                         "bdb_modrdn: can't figure out "
638                         "type(s)/values(s) of newrdn\n", 
639                         0, 0, 0 );
640 #else
641                 Debug( LDAP_DEBUG_TRACE,
642                         "bdb_modrdn: can't figure out "
643                         "type(s)/values(s) of newrdn\n", 
644                         0, 0, 0 );
645 #endif
646                 rc = LDAP_INVALID_DN_SYNTAX;
647                 text = "unknown type(s) used in RDN";
648                 goto return_results;
649         }
650
651 #ifdef NEW_LOGGING
652         LDAP_LOG ( OPERATION, RESULTS, 
653                 "bdb_modrdn: new_rdn_type=\"%s\", "
654                 "new_rdn_val=\"%s\"\n",
655                 new_rdn[ 0 ][ 0 ]->la_attr.bv_val, 
656                 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
657 #else
658         Debug( LDAP_DEBUG_TRACE,
659                 "bdb_modrdn: new_rdn_type=\"%s\", "
660                 "new_rdn_val=\"%s\"\n",
661                 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
662                 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
663 #endif
664
665         if ( deleteoldrdn ) {
666                 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
667                         LDAP_DN_FORMAT_LDAP ) )
668                 {
669 #ifdef NEW_LOGGING
670                         LDAP_LOG ( OPERATION, ERR, 
671                                 "bdb_modrdn: can't figure out "
672                                 "type(s)/values(s) of old_rdn\n", 
673                                 0, 0, 0 );
674 #else
675                         Debug( LDAP_DEBUG_TRACE,
676                                 "bdb_modrdn: can't figure out "
677                                 "the old_rdn type(s)/value(s)\n", 
678                                 0, 0, 0 );
679 #endif
680                         rc = LDAP_OTHER;
681                         text = "cannot parse RDN from old DN";
682                         goto return_results;            
683                 }
684         }
685
686         /* prepare modlist of modifications from old/new rdn */
687         rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn, 
688                         deleteoldrdn, &mod );
689         if ( rc != LDAP_SUCCESS ) {
690                 goto return_results;
691         }
692         
693         /* delete old one */
694         rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
695         if ( rc != 0 ) {
696                 switch( rc ) {
697                 case DB_LOCK_DEADLOCK:
698                 case DB_LOCK_NOTGRANTED:
699                         goto retry;
700                 }
701                 rc = LDAP_OTHER;
702                 text = "DN index delete fail";
703                 goto return_results;
704         }
705
706         (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
707
708         /* Binary format uses a single contiguous block, cannot
709          * free individual fields. Leave new_dn/new_ndn set so
710          * they can be individually freed later.
711          */
712         e->e_name = new_dn;
713         e->e_nname = new_ndn;
714
715         new_dn.bv_val = NULL;
716         new_ndn.bv_val = NULL;
717
718         /* add new one */
719         rc = bdb_dn2id_add( be, ltid, np_ndn, e );
720         if ( rc != 0 ) {
721                 switch( rc ) {
722                 case DB_LOCK_DEADLOCK:
723                 case DB_LOCK_NOTGRANTED:
724                         goto retry;
725                 }
726                 rc = LDAP_OTHER;
727                 text = "DN index add failed";
728                 goto return_results;
729         }
730
731 #ifdef LDAP_CLIENT_UPDATE
732         if ( rc == LDAP_SUCCESS && !op->o_noop ) {
733                 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
734                         bdb_psearch(be, conn, op, ps_list, e, LCUP_PSEARCH_BY_PREMODIFY );
735                 }
736         }
737 #endif /* LDAP_CLIENT_UPDATE */
738
739         /* modify entry */
740         rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
741                 &text, textbuf, textlen );
742
743         if( rc != LDAP_SUCCESS ) {
744                 switch( rc ) {
745                 case DB_LOCK_DEADLOCK:
746                 case DB_LOCK_NOTGRANTED:
747                         goto retry;
748                 }
749                 goto return_results;
750         }
751         
752         /* id2entry index */
753         rc = bdb_id2entry_update( be, ltid, e );
754         if ( rc != 0 ) {
755                 switch( rc ) {
756                 case DB_LOCK_DEADLOCK:
757                 case DB_LOCK_NOTGRANTED:
758                         goto retry;
759                 }
760                 rc = LDAP_OTHER;
761                 text = "entry update failed";
762                 goto return_results;
763         }
764
765         if( op->o_noop ) {
766                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
767                         text = "txn_abort (no-op) failed";
768                 } else {
769                         noop = 1;
770                         rc = LDAP_SUCCESS;
771                 }
772
773         } else {
774                 char gid[DB_XIDDATASIZE];
775
776                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
777                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
778
779                 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
780                         text = "txn_prepare failed";
781                 } else {
782                         if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
783                                 if(( rc=TXN_ABORT( ltid )) != 0 ) {
784                                         text ="cache update & txn_abort failed";
785                                 } else {
786                                         rc = LDAP_OTHER;
787                                         text = "cache update failed";
788                                 }
789
790                         } else {
791                                 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
792                                         text = "txn_commit failed";
793                                 } else {
794                                         rc = LDAP_SUCCESS;
795                                 }
796                         }
797                 }
798         }
799  
800         ltid = NULL;
801         op->o_private = NULL;
802  
803         if( rc == LDAP_SUCCESS ) {
804 #ifdef NEW_LOGGING
805                 LDAP_LOG ( OPERATION, RESULTS, 
806                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n", 
807                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
808 #else
809                 Debug(LDAP_DEBUG_TRACE,
810                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
811                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
812 #endif
813                 text = NULL;
814                 if ( !noop ) {
815                         bdb_cache_entry_commit( e );
816                 }
817
818         } else {
819 #ifdef NEW_LOGGING
820                 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n", 
821                         text, db_strerror(rc), rc );
822 #else
823                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
824                         text, db_strerror(rc), rc );
825 #endif
826                 rc = LDAP_OTHER;
827         }
828
829 return_results:
830         send_ldap_result( conn, op, rc,
831                 NULL, text, NULL, NULL );
832
833 #ifdef LDAP_CLIENT_UPDATE
834         if ( rc == LDAP_SUCCESS && !op->o_noop ) {
835                 /* Loop through in-scope entries for each psearch spec */
836                 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
837                         bdb_psearch( be, conn, op, ps_list, e, LCUP_PSEARCH_BY_MODIFY );
838                 }
839                 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
840                 while ( pm_list != NULL ) {
841                         bdb_psearch(be, conn, op, pm_list->ps->op,
842                                                 e, LCUP_PSEARCH_BY_SCOPEOUT);
843                         LDAP_LIST_REMOVE ( pm_list, link );
844                         pm_prev = pm_list;
845                         pm_list = LDAP_LIST_NEXT ( pm_list, link );
846                         free (pm_prev);
847                 }
848         }
849 #endif /* LDAP_CLIENT_UPDATE */
850
851         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
852                 ldap_pvt_thread_yield();
853                 TXN_CHECKPOINT( bdb->bi_dbenv,
854                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
855         }
856
857 done:
858         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
859         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
860
861         /* LDAP v2 supporting correct attribute handling. */
862         if ( new_rdn != NULL ) {
863                 ldap_rdnfree( new_rdn );
864         }
865         if ( old_rdn != NULL ) {
866                 ldap_rdnfree( old_rdn );
867         }
868         if( mod != NULL ) {
869                 Modifications *tmp;
870                 for (; mod; mod=tmp ) {
871                         tmp = mod->sml_next;
872                         free( mod );
873                 }
874         }
875
876         /* LDAP v3 Support */
877         if( np != NULL ) {
878                 /* free new parent and reader lock */
879                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
880         }
881
882         if( p != NULL ) {
883                 /* free parent and reader lock */
884                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
885         }
886
887         /* free entry */
888         if( e != NULL ) {
889                 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
890         }
891
892         if( ltid != NULL ) {
893 #ifdef LDAP_CLIENT_UPDATE
894                 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
895                 while ( pm_list != NULL ) {
896                         LDAP_LIST_REMOVE ( pm_list, link );
897                         pm_prev = pm_list;
898                         pm_list = LDAP_LIST_NEXT ( pm_list, link );
899                         free (pm_prev);
900                 }
901 #endif
902                 TXN_ABORT( ltid );
903                 op->o_private = NULL;
904         }
905
906         return ( ( rc == LDAP_SUCCESS ) ? noop : rc );
907 }