]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/modrdn.c
comment cleanup
[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_cache_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_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                 dnNormalize( 0, NULL, 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_ndn ( 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, eip, e,
779                 op->o_tmpmemctx );
780         if ( rs->sr_err != 0 ) {
781 #ifdef NEW_LOGGING
782                 LDAP_LOG ( OPERATION, ERR, 
783                         "<=- bdb_modrdn: dn2id del failed: %s (%d)\n",
784                         db_strerror(rs->sr_err), rs->sr_err, 0 );
785 #else
786                 Debug(LDAP_DEBUG_TRACE,
787                         "<=- bdb_modrdn: dn2id del failed: %s (%d)\n",
788                         db_strerror(rs->sr_err), rs->sr_err, 0 );
789 #endif
790                 switch( rs->sr_err ) {
791                 case DB_LOCK_DEADLOCK:
792                 case DB_LOCK_NOTGRANTED:
793                         goto retry;
794                 }
795                 rs->sr_err = LDAP_OTHER;
796                 rs->sr_text = "DN index delete fail";
797                 goto return_results;
798         }
799
800         /* Binary format uses a single contiguous block, cannot
801          * free individual fields. But if a previous modrdn has
802          * already happened, must free the names. The frees are
803          * done in bdb_cache_modrdn().
804          */
805 #ifdef BDB_HIER
806         e->e_name.bv_val = ch_malloc(new_dn.bv_len + new_ndn.bv_len + 2);
807         e->e_name.bv_len = new_dn.bv_len;
808         e->e_nname.bv_val = e->e_name.bv_val + new_dn.bv_len + 1;
809         e->e_nname.bv_len = new_ndn.bv_len;
810         strcpy(e->e_name.bv_val, new_dn.bv_val);
811         strcpy(e->e_nname.bv_val, new_ndn.bv_val);
812 #else
813         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
814                 e->e_bv.bv_val + e->e_bv.bv_len ) {
815                 e->e_name.bv_val = NULL;
816                 e->e_nname.bv_val = NULL;
817         }
818         e->e_name = new_dn;
819         e->e_nname = new_ndn;
820         new_dn.bv_val = NULL;
821         new_ndn.bv_val = NULL;
822 #endif
823         /* add new one */
824         rs->sr_err = bdb_dn2id_add( op->o_bd, lt2, neip ? neip : eip, e,
825                 op->o_tmpmemctx );
826         if ( rs->sr_err != 0 ) {
827 #ifdef NEW_LOGGING
828                 LDAP_LOG ( OPERATION, ERR, 
829                         "<=- bdb_modrdn: dn2id add failed: %s (%d)\n",
830                         db_strerror(rs->sr_err), rs->sr_err, 0 );
831 #else
832                 Debug(LDAP_DEBUG_TRACE,
833                         "<=- bdb_modrdn: dn2id add failed: %s (%d)\n",
834                         db_strerror(rs->sr_err), rs->sr_err, 0 );
835 #endif
836                 switch( rs->sr_err ) {
837                 case DB_LOCK_DEADLOCK:
838                 case DB_LOCK_NOTGRANTED:
839                         goto retry;
840                 }
841                 rs->sr_err = LDAP_OTHER;
842                 rs->sr_text = "DN index add failed";
843                 goto return_results;
844         }
845
846 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
847         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
848                 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
849                         bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_PREMODIFY );
850                 }
851         }
852 #endif
853
854         /* modify entry */
855         rs->sr_err = bdb_modify_internal( op, lt2, &mod[0], e,
856                 &rs->sr_text, textbuf, textlen );
857
858         if( rs->sr_err != LDAP_SUCCESS ) {
859 #ifdef NEW_LOGGING
860                 LDAP_LOG ( OPERATION, ERR, 
861                         "<=- bdb_modrdn: modify failed: %s (%d)\n",
862                         db_strerror(rs->sr_err), rs->sr_err, 0 );
863 #else
864                 Debug(LDAP_DEBUG_TRACE,
865                         "<=- bdb_modrdn: modify failed: %s (%d)\n",
866                         db_strerror(rs->sr_err), rs->sr_err, 0 );
867 #endif
868                 if ( ( rs->sr_err == LDAP_INSUFFICIENT_ACCESS ) && opinfo.boi_err ) {
869                         rs->sr_err = opinfo.boi_err;
870                 }
871                 switch( rs->sr_err ) {
872                 case DB_LOCK_DEADLOCK:
873                 case DB_LOCK_NOTGRANTED:
874                         goto retry;
875                 }
876                 goto return_results;
877         }
878         
879         /* id2entry index */
880         rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, e );
881         if ( rs->sr_err != 0 ) {
882 #ifdef NEW_LOGGING
883                 LDAP_LOG ( OPERATION, ERR, 
884                         "<=- bdb_modrdn: id2entry failed: %s (%d)\n",
885                         db_strerror(rs->sr_err), rs->sr_err, 0 );
886 #else
887                 Debug(LDAP_DEBUG_TRACE,
888                         "<=- bdb_modrdn: id2entry failed: %s (%d)\n",
889                         db_strerror(rs->sr_err), rs->sr_err, 0 );
890 #endif
891                 switch( rs->sr_err ) {
892                 case DB_LOCK_DEADLOCK:
893                 case DB_LOCK_NOTGRANTED:
894                         goto retry;
895                 }
896                 rs->sr_err = LDAP_OTHER;
897                 rs->sr_text = "entry update failed";
898                 goto return_results;
899         }
900         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
901                 rs->sr_err = LDAP_OTHER;
902                 rs->sr_text = "txn_commit(2) failed";
903                 goto return_results;
904         }
905
906         if( op->o_noop ) {
907                 if(( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
908                         rs->sr_text = "txn_abort (no-op) failed";
909                 } else {
910                         noop = 1;
911                         rs->sr_err = LDAP_SUCCESS;
912                 }
913
914         } else {
915                 char gid[DB_XIDDATASIZE];
916
917                 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
918                         bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
919
920                 if(( rs->sr_err=TXN_PREPARE( ltid, gid )) != 0 ) {
921                         rs->sr_text = "txn_prepare failed";
922                 } else {
923                         bdb_cache_modrdn( save, &op->orr_nnewrdn, e, neip,
924                                 bdb->bi_dbenv, locker, &lock );
925                         if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
926                                 rs->sr_text = "txn_commit failed";
927                         } else {
928                                 rs->sr_err = LDAP_SUCCESS;
929                         }
930                 }
931         }
932  
933         ltid = NULL;
934         op->o_private = NULL;
935  
936         if( rs->sr_err == LDAP_SUCCESS ) {
937 #ifdef NEW_LOGGING
938                 LDAP_LOG ( OPERATION, RESULTS, 
939                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n", 
940                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
941 #else
942                 Debug(LDAP_DEBUG_TRACE,
943                         "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
944                         op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
945 #endif
946                 rs->sr_text = NULL;
947         } else {
948 #ifdef NEW_LOGGING
949                 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n", 
950                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
951 #else
952                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
953                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
954 #endif
955                 rs->sr_err = LDAP_OTHER;
956         }
957
958 return_results:
959         send_ldap_result( op, rs );
960
961 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
962         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
963                 /* Loop through in-scope entries for each psearch spec */
964                 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
965                         bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
966                 }
967                 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
968                 while ( pm_list != NULL ) {
969                         bdb_psearch(op, rs, pm_list->ps_op,
970                                                 e, LDAP_PSEARCH_BY_SCOPEOUT);
971                         pm_prev = pm_list;
972                         LDAP_LIST_REMOVE ( pm_list, ps_link );
973                         pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
974                         ch_free( pm_prev );
975                 }
976         }
977 #endif
978
979         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
980                 ldap_pvt_thread_yield();
981                 TXN_CHECKPOINT( bdb->bi_dbenv,
982                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
983         }
984
985 done:
986         if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
987         if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
988
989         /* LDAP v2 supporting correct attribute handling. */
990         if ( new_rdn != NULL ) {
991                 ldap_rdnfree_x( new_rdn, op->o_tmpmemctx );
992         }
993         if ( old_rdn != NULL ) {
994                 ldap_rdnfree_x( old_rdn, op->o_tmpmemctx );
995         }
996         if( mod != NULL ) {
997                 Modifications *tmp;
998                 for (; mod; mod=tmp ) {
999                         tmp = mod->sml_next;
1000                         if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
1001                         free( mod );
1002                 }
1003         }
1004
1005         /* LDAP v3 Support */
1006         if( np != NULL ) {
1007                 /* free new parent and reader lock */
1008                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
1009         }
1010
1011         if( p != NULL ) {
1012                 /* free parent and reader lock */
1013                 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
1014         }
1015
1016         /* free entry */
1017         if( e != NULL ) {
1018                 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
1019         }
1020
1021         if( ltid != NULL ) {
1022 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
1023                 pm_list = LDAP_LIST_FIRST(&op->o_pm_list);
1024                 while ( pm_list != NULL ) {
1025                         LDAP_LIST_REMOVE ( pm_list, ps_link );
1026                         pm_prev = pm_list;
1027                         pm_list = LDAP_LIST_NEXT ( pm_list, ps_link );
1028                         ch_free( pm_prev );
1029                 }
1030 #endif
1031                 TXN_ABORT( ltid );
1032                 op->o_private = NULL;
1033         }
1034
1035         return ( ( rs->sr_err == LDAP_SUCCESS ) ? noop : rs->sr_err );
1036 }