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