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