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