/
ldbm_modrdn.c
2066 lines (1943 loc) · 81.1 KB
/
ldbm_modrdn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* In addition, as a special exception, Red Hat, Inc. gives You the additional
* right to link the code of this Program with code not covered under the GNU
* General Public License ("Non-GPL Code") and to distribute linked combinations
* including the two, subject to the limitations in this paragraph. Non-GPL Code
* permitted under this exception must only link to the code of this Program
* through those well defined interfaces identified in the file named EXCEPTION
* found in the source code files (the "Approved Interfaces"). The files of
* Non-GPL Code may instantiate templates or use macros or inline functions from
* the Approved Interfaces without causing the resulting work to be covered by
* the GNU General Public License. Only Red Hat, Inc. may make changes or
* additions to the list of Approved Interfaces. You must obey the GNU General
* Public License in all respects for all of the Program code and other code used
* in conjunction with the Program except the Non-GPL Code covered by this
* exception. If you modify this file, you may extend this exception to your
* version of the file, but you are not obligated to do so. If you do not wish to
* provide this exception without modification, you must delete this exception
* statement from your version and license this file solely under the GPL without
* exception.
*
*
* Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
* Copyright (C) 2005 Red Hat, Inc.
* Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
* All rights reserved.
* END COPYRIGHT BLOCK **/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* modrdn.c - ldbm backend modrdn routine */
#include "back-ldbm.h"
static const char *moddn_get_newdn(Slapi_PBlock *pb, Slapi_DN *dn_olddn, Slapi_DN *dn_newrdn, Slapi_DN *dn_newsuperiordn);
static void moddn_unlock_and_return_entry(backend *be,struct backentry **targetentry);
static int moddn_newrdn_mods(Slapi_PBlock *pb, const char *olddn, struct backentry *ec, Slapi_Mods *smods_wsi, int is_repl_op);
static IDList *moddn_get_children(back_txn *ptxn, Slapi_PBlock *pb, backend *be, struct backentry *parententry, Slapi_DN *parentdn, struct backentry ***child_entries, struct backdn ***child_dns);
static int moddn_rename_children(back_txn *ptxn, Slapi_PBlock *pb, backend *be, IDList *children, Slapi_DN *dn_parentdn, Slapi_DN *dn_newsuperiordn, struct backentry *child_entries[]);
static int modrdn_rename_entry_update_indexes(back_txn *ptxn, Slapi_PBlock *pb, struct ldbminfo *li, struct backentry *e, struct backentry **ec, Slapi_Mods *smods1, Slapi_Mods *smods2, Slapi_Mods *smods3, int *e_in_cache, int *ec_in_cache);
static void mods_remove_nsuniqueid(Slapi_Mods *smods);
#define MOD_SET_ERROR(rc, error, count) \
{ \
(rc) = (error); \
(count) = RETRY_TIMES; /* otherwise, the transaction may not be aborted */ \
}
int
ldbm_back_modrdn( Slapi_PBlock *pb )
{
backend *be;
ldbm_instance *inst = NULL;
struct ldbminfo *li;
struct backentry *e= NULL;
struct backentry *ec= NULL;
int ec_in_cache= 0;
int e_in_cache= 0;
back_txn txn;
back_txnid parent_txn;
int retval = -1;
char *msg;
Slapi_Entry *postentry = NULL;
char *errbuf = NULL;
int disk_full = 0;
int retry_count = 0;
int ldap_result_code= LDAP_SUCCESS;
char *ldap_result_message= NULL;
char *ldap_result_matcheddn= NULL;
struct backentry *parententry= NULL;
struct backentry *newparententry= NULL;
struct backentry *original_entry = NULL;
struct backentry *original_parent = NULL;
struct backentry *original_newparent = NULL;
modify_context parent_modify_context = {0};
modify_context newparent_modify_context = {0};
modify_context ruv_c = {0};
int ruv_c_init = 0;
int is_ruv = 0;
IDList *children= NULL;
struct backentry **child_entries = NULL;
struct backdn **child_dns = NULL;
Slapi_DN *sdn = NULL;
Slapi_DN dn_newdn;
Slapi_DN dn_newrdn;
Slapi_DN *dn_newsuperiordn = NULL;
Slapi_DN dn_parentdn;
Slapi_DN *orig_dn_newsuperiordn = NULL;
Slapi_Entry *target_entry = NULL;
Slapi_Entry *original_targetentry = NULL;
int rc;
int isroot;
LDAPMod **mods;
Slapi_Mods smods_operation_wsi = {0};
Slapi_Mods smods_generated = {0};
Slapi_Mods smods_generated_wsi = {0};
Slapi_Operation *operation;
int is_replicated_operation= 0;
int is_fixup_operation = 0;
entry_address new_addr;
entry_address *old_addr;
entry_address oldparent_addr;
entry_address *newsuperior_addr;
char *original_newrdn = NULL;
CSN *opcsn = NULL;
const char *newdn = NULL;
char *newrdn = NULL;
int opreturn = 0;
int free_modrdn_existing_entry = 0;
/* sdn & parentsdn need to be initialized before "goto *_return" */
slapi_sdn_init(&dn_newdn);
slapi_sdn_init(&dn_newrdn);
slapi_sdn_init(&dn_parentdn);
slapi_pblock_get( pb, SLAPI_MODRDN_TARGET_SDN, &sdn );
slapi_pblock_get( pb, SLAPI_BACKEND, &be);
slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
slapi_pblock_get( pb, SLAPI_TXN, (void**)&parent_txn );
slapi_pblock_get( pb, SLAPI_REQUESTOR_ISROOT, &isroot );
slapi_pblock_get( pb, SLAPI_OPERATION, &operation );
slapi_pblock_get( pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation );
is_ruv = operation_is_flag_set(operation, OP_FLAG_REPL_RUV);
is_fixup_operation = operation_is_flag_set(operation, OP_FLAG_REPL_FIXUP);
if (NULL == sdn) {
slapi_send_ldap_result( pb, LDAP_INVALID_DN_SYNTAX, NULL,
"Null target DN", 0, NULL );
return( -1 );
}
/* dblayer_txn_init needs to be called before "goto error_return" */
dblayer_txn_init(li,&txn);
/* the calls to perform searches require the parent txn if any
so set txn to the parent_txn until we begin the child transaction */
if (parent_txn) {
txn.back_txn_txn = parent_txn;
} else {
parent_txn = txn.back_txn_txn;
slapi_pblock_set( pb, SLAPI_TXN, parent_txn );
}
if (pb->pb_conn)
{
slapi_log_error (SLAPI_LOG_TRACE, "ldbm_back_modrdn", "enter conn=%" NSPRIu64 " op=%d\n", pb->pb_conn->c_connid, operation->o_opid);
}
inst = (ldbm_instance *) be->be_instance_info;
{
char *newrdn/* , *newsuperiordn */;
/* newrdn is normalized, bu tno need to be case-ignored as
* it's passed to slapi_sdn_init_normdn_byref */
slapi_pblock_get( pb, SLAPI_MODRDN_NEWRDN, &newrdn );
slapi_pblock_get( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &dn_newsuperiordn );
slapi_sdn_init_normdn_byref(&dn_newrdn, newrdn);
/* slapi_sdn_init_normdn_byref(&dn_newsuperiordn, newsuperiordn); */
slapi_sdn_get_parent(sdn, &dn_parentdn);
}
/* if old and new superior are equals, newsuperior should not be set
* Here we have to reset newsuperiordn in order to save processing and
* avoid later deadlock when trying to fetch twice the same entry
*/
if (slapi_sdn_compare(dn_newsuperiordn, &dn_parentdn) == 0)
{
slapi_sdn_done(dn_newsuperiordn);
}
/*
* Even if subtree rename is off,
* Replicated Operations are allowed to change the superior
*/
if ( !entryrdn_get_switch() &&
(!is_replicated_operation && !slapi_sdn_isempty(dn_newsuperiordn)))
{
slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
"server does not support moving of entries", 0, NULL );
return( -1 );
}
if (inst && inst->inst_ref_count) {
slapi_counter_increment(inst->inst_ref_count);
} else {
LDAPDebug1Arg(LDAP_DEBUG_ANY,
"ldbm_modrdn: instance \"%s\" does not exist.\n",
inst ? inst->inst_name : "null instance");
return( -1 );
}
/* The dblock serializes writes to the database,
* which reduces deadlocking in the db code,
* which means that we run faster.
*
* But, this lock is re-enterant for the fixup
* operations that the URP code in the Replication
* plugin generates.
*
* Also some URP post-op operations are called after
* the backend has committed the change and released
* the dblock. Acquire the dblock again for them
* if OP_FLAG_ACTION_INVOKE_FOR_REPLOP is set.
*
* SERIALLOCK is moved to dblayer_txn_begin along with exposing be
* transaction to plugins (see slapi_back_transaction_* APIs).
*
if(SERIALLOCK(li) &&
(!operation_is_flag_set(operation,OP_FLAG_REPL_FIXUP) ||
operation_is_flag_set(operation,OP_FLAG_ACTION_INVOKE_FOR_REPLOP)))
{
dblayer_lock_backend(be);
dblock_acquired= 1;
}
*/
/*
* So, we believe that no code up till here actually added anything
* to persistent store. From now on, we're transacted
*/
txn.back_txn_txn = NULL; /* ready to create the child transaction */
for (retry_count = 0; retry_count < RETRY_TIMES; retry_count++)
{
if (txn.back_txn_txn && (txn.back_txn_txn != parent_txn))
{
Slapi_Entry *ent = NULL;
/* don't release SERIAL LOCK */
dblayer_txn_abort_ext(li, &txn, PR_FALSE);
/* txn is no longer valid - reset slapi_txn to the parent */
slapi_pblock_set(pb, SLAPI_TXN, parent_txn);
slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
slapi_ch_free_string(&newrdn);
slapi_pblock_set(pb, SLAPI_MODRDN_NEWRDN, original_newrdn);
slapi_sdn_set_normdn_byref(&dn_newrdn, original_newrdn);
original_newrdn = slapi_ch_strdup(original_newrdn);
slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &dn_newsuperiordn);
slapi_sdn_free(&dn_newsuperiordn);
slapi_pblock_set(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, orig_dn_newsuperiordn);
orig_dn_newsuperiordn = slapi_sdn_dup(orig_dn_newsuperiordn);
if (ec_in_cache) {
/* New entry 'ec' is in the entry cache.
* Remove it from the cache . */
CACHE_REMOVE(&inst->inst_cache, ec);
CACHE_RETURN(&inst->inst_cache, &ec);
#ifdef DEBUG_CACHE
PR_ASSERT(ec == NULL);
#endif
ec_in_cache = 0;
} else {
backentry_free(&ec);
}
/* make sure the original entry is back in the cache if it was removed */
if (!e_in_cache) {
CACHE_ADD(&inst->inst_cache, e, NULL);
e_in_cache = 1;
}
slapi_pblock_get( pb, SLAPI_MODRDN_EXISTING_ENTRY, &ent );
if (ent && (ent != original_entry->ep_entry)) {
slapi_entry_free(ent);
slapi_pblock_set( pb, SLAPI_MODRDN_EXISTING_ENTRY, NULL );
}
ec = original_entry;
if ( (original_entry = backentry_dup( ec )) == NULL ) {
ldap_result_code= LDAP_OPERATIONS_ERROR;
goto error_return;
}
slapi_pblock_set( pb, SLAPI_MODRDN_EXISTING_ENTRY, original_entry->ep_entry );
free_modrdn_existing_entry = 0; /* owned by original_entry now */
if (!ec_in_cache) {
/* Put the resetted entry 'ec' into the cache again. */
if (cache_add_tentative( &inst->inst_cache, ec, NULL ) != 0) {
ec_in_cache = 0; /* not in cache */
/* allow modrdn even if the src dn and dest dn are identical */
if ( 0 != slapi_sdn_compare((const Slapi_DN *)&dn_newdn,
(const Slapi_DN *)sdn) ) {
LDAPDebug1Arg(LDAP_DEBUG_ANY,
"ldbm_back_modrdn: adding %s to cache failed\n",
slapi_entry_get_dn_const(ec->ep_entry));
ldap_result_code = LDAP_OPERATIONS_ERROR;
goto error_return;
}
/* so if the old dn is the same as the new dn, the entry will not be cached
until it is replaced with cache_replace */
} else {
ec_in_cache = 1;
}
}
slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_ENTRY, &target_entry );
slapi_entry_free(target_entry);
slapi_pblock_set( pb, SLAPI_MODRDN_TARGET_ENTRY, original_targetentry );
if ( (original_targetentry = slapi_entry_dup( original_targetentry )) == NULL ) {
ldap_result_code= LDAP_OPERATIONS_ERROR;
goto error_return;
}
/* We're re-trying */
LDAPDebug0Args(LDAP_DEBUG_BACKLDBM,
"Modrdn Retrying Transaction\n");
#ifndef LDBM_NO_BACKOFF_DELAY
{
PRIntervalTime interval;
interval = PR_MillisecondsToInterval(slapi_rand() % 100);
DS_Sleep(interval);
}
#endif
}
if (0 == retry_count) {
/* First time, hold SERIAL LOCK */
retval = dblayer_txn_begin(be, parent_txn, &txn);
} else {
/* Otherwise, no SERIAL LOCK */
retval = dblayer_txn_begin_ext(li, parent_txn, &txn, PR_FALSE);
}
if (0 != retval) {
ldap_result_code= LDAP_OPERATIONS_ERROR;
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
goto error_return;
}
/* stash the transaction */
slapi_pblock_set(pb, SLAPI_TXN, (void *)txn.back_txn_txn);
if (0 == retry_count) { /* just once */
rc = 0;
rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY);
rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY);
rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY);
rc= slapi_setbit_int(rc,SLAPI_RTN_BIT_FETCH_TARGET_ENTRY);
while(rc!=0)
{
/* JCM - copying entries can be expensive... should optimize */
/*
* Some present state information is passed through the PBlock to the
* backend pre-op plugin. To ensure a consistent snapshot of this state
* we wrap the reading of the entry with the dblock.
*/
/* <new rdn>,<new superior> */
if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_EXISTING_DN_ENTRY))
{
/* see if an entry with the new name already exists */
done_with_pblock_entry(pb,SLAPI_MODRDN_EXISTING_ENTRY); /* Could be through this multiple times */
/* newrdn is normalized, bu tno need to be case-ignored as
* it's passed to slapi_sdn_init_normdn_byref */
slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
slapi_sdn_init_normdn_byref(&dn_newrdn, newrdn);
newdn= moddn_get_newdn(pb,sdn, &dn_newrdn, dn_newsuperiordn);
slapi_sdn_set_dn_passin(&dn_newdn,newdn);
new_addr.sdn = &dn_newdn;
new_addr.udn = NULL;
/* check dn syntax on newdn */
ldap_result_code = slapi_dn_syntax_check(pb,
(char *)slapi_sdn_get_ndn(&dn_newdn), 1);
if (ldap_result_code)
{
ldap_result_code = LDAP_INVALID_DN_SYNTAX;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
goto error_return;
}
new_addr.uniqueid = NULL;
ldap_result_code= get_copy_of_entry(pb, &new_addr, &txn, SLAPI_MODRDN_EXISTING_ENTRY, 0);
if(ldap_result_code==LDAP_OPERATIONS_ERROR ||
ldap_result_code==LDAP_INVALID_DN_SYNTAX)
{
goto error_return;
}
free_modrdn_existing_entry = 1; /* need to free it */
}
/* <old superior> */
if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_PARENT_ENTRY))
{
/* find and lock the old parent entry */
done_with_pblock_entry(pb,SLAPI_MODRDN_PARENT_ENTRY); /* Could be through this multiple times */
oldparent_addr.sdn = &dn_parentdn;
oldparent_addr.uniqueid = NULL;
ldap_result_code= get_copy_of_entry(pb, &oldparent_addr, &txn, SLAPI_MODRDN_PARENT_ENTRY, !is_replicated_operation);
}
/* <new superior> */
if(slapi_sdn_get_ndn(dn_newsuperiordn)!=NULL &&
slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_NEWPARENT_ENTRY))
{
/* find and lock the new parent entry */
done_with_pblock_entry(pb,SLAPI_MODRDN_NEWPARENT_ENTRY); /* Could be through this multiple times */
/* Check that this really is a new superior,
* and not the same old one. Compare parentdn & newsuperior */
if (slapi_sdn_compare(dn_newsuperiordn, &dn_parentdn) == 0)
{
slapi_sdn_done(dn_newsuperiordn);
}
else
{
entry_address my_addr;
if (is_replicated_operation)
{
/* If this is a replicated operation,
* then should fetch new superior with uniqueid */
slapi_pblock_get (pb, SLAPI_MODRDN_NEWSUPERIOR_ADDRESS,
&newsuperior_addr);
}
else
{
my_addr.sdn = dn_newsuperiordn;
my_addr.uniqueid = NULL;
newsuperior_addr = &my_addr;
}
ldap_result_code= get_copy_of_entry(pb, newsuperior_addr, &txn, SLAPI_MODRDN_NEWPARENT_ENTRY, !is_replicated_operation);
}
}
/* <old rdn>,<old superior> */
if(slapi_isbitset_int(rc,SLAPI_RTN_BIT_FETCH_TARGET_ENTRY))
{
/* find and lock the entry we are about to modify */
done_with_pblock_entry(pb,SLAPI_MODRDN_TARGET_ENTRY); /* Could be through this multiple times */
slapi_pblock_get (pb, SLAPI_TARGET_ADDRESS, &old_addr);
ldap_result_code= get_copy_of_entry(pb, old_addr, &txn, SLAPI_MODRDN_TARGET_ENTRY, !is_replicated_operation);
if(ldap_result_code==LDAP_OPERATIONS_ERROR ||
ldap_result_code==LDAP_INVALID_DN_SYNTAX)
{
/* JCM - Usually the call to find_entry2modify would generate the result code. */
/* JCM !!! */
goto error_return;
}
}
/* Call the Backend Pre ModRDN plugins */
slapi_pblock_set(pb, SLAPI_RESULT_CODE, &ldap_result_code);
rc= plugin_call_plugins(pb, SLAPI_PLUGIN_BE_PRE_MODRDN_FN);
if(rc==-1)
{
/*
* Plugin indicated some kind of failure,
* or that this Operation became a No-Op.
*/
if (!ldap_result_code) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
}
if (!opreturn) {
slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
}
if (!opreturn) {
slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &rc );
}
goto error_return;
}
/*
* (rc!=-1) means that the plugin changed things, so we go around
* the loop once again to get the new present state.
*/
/* JCMREPL - Warning: A Plugin could cause an infinite loop by always returning a result code that requires some action. */
}
/* find and lock the entry we are about to modify */
/* JCMREPL - Argh, what happens about the stinking referrals? */
slapi_pblock_get (pb, SLAPI_TARGET_ADDRESS, &old_addr);
e = find_entry2modify( pb, be, old_addr, &txn );
if ( e == NULL )
{
ldap_result_code= -1;
goto error_return; /* error result sent by find_entry2modify() */
}
e_in_cache = 1; /* e is in the cache and locked */
/* Check that an entry with the same DN doesn't already exist. */
{
Slapi_Entry *entry;
slapi_pblock_get( pb, SLAPI_MODRDN_EXISTING_ENTRY, &entry);
if((entry != NULL) &&
/* allow modrdn even if the src dn and dest dn are identical */
(0 != slapi_sdn_compare((const Slapi_DN *)&dn_newdn,
(const Slapi_DN *)sdn)))
{
ldap_result_code= LDAP_ALREADY_EXISTS;
goto error_return;
}
}
/* Fetch and lock the parent of the entry that is moving */
oldparent_addr.sdn = &dn_parentdn;
oldparent_addr.uniqueid = NULL;
parententry = find_entry2modify_only( pb, be, &oldparent_addr, &txn );
modify_init(&parent_modify_context,parententry);
/* Fetch and lock the new parent of the entry that is moving */
if(slapi_sdn_get_ndn(dn_newsuperiordn) != NULL)
{
slapi_pblock_get (pb, SLAPI_MODRDN_NEWSUPERIOR_ADDRESS, &newsuperior_addr);
newparententry = find_entry2modify_only( pb, be, newsuperior_addr, &txn );
modify_init(&newparent_modify_context,newparententry);
}
opcsn = operation_get_csn (operation);
if (!is_fixup_operation)
{
if ( opcsn == NULL && operation->o_csngen_handler)
{
/*
* Current op is a user request. Opcsn will be assigned
* if the dn is in an updatable replica.
*/
opcsn = entry_assign_operation_csn ( pb, e->ep_entry, parententry ? parententry->ep_entry : NULL );
}
if ( opcsn != NULL )
{
entry_set_maxcsn (e->ep_entry, opcsn);
}
}
/*
* Now that we have the old entry, we reset the old DN and recompute
* the new DN. Why? Because earlier when we computed the new DN, we did
* not have the old entry, so we used the DN that was presented as the
* target DN in the ModRDN operation itself, and we would prefer to
* preserve the case and spacing that are in the actual entry's DN
* instead. Otherwise, a ModRDN operation will potentially change an
* entry's entire DN (at least with respect to case and spacing).
*/
slapi_sdn_copy( slapi_entry_get_sdn_const( e->ep_entry ), sdn );
slapi_pblock_set( pb, SLAPI_MODRDN_TARGET_SDN, sdn );
if (newparententry != NULL) {
/* don't forget we also want to preserve case of new superior */
if (NULL == dn_newsuperiordn) {
dn_newsuperiordn = slapi_sdn_dup(
slapi_entry_get_sdn_const(newparententry->ep_entry));
} else {
slapi_sdn_copy(slapi_entry_get_sdn_const(newparententry->ep_entry),
dn_newsuperiordn);
}
slapi_pblock_set( pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, dn_newsuperiordn );
}
slapi_sdn_set_dn_passin(&dn_newdn,
moddn_get_newdn(pb, sdn, &dn_newrdn, dn_newsuperiordn));
/* Check that we're allowed to add an entry below the new superior */
if ( newparententry == NULL )
{
/* There may not be a new parent because we don't intend there to be one. */
if (slapi_sdn_get_dn(dn_newsuperiordn))
{
/* If the new entry is not to be a suffix,
* return an error no matter who requested this modrdn */
if (!slapi_be_issuffix(pb->pb_backend, &dn_newdn))
{
/* Here means that we didn't find the parent */
int err = 0;
Slapi_DN ancestorsdn;
struct backentry *ancestorentry;
slapi_sdn_init(&ancestorsdn);
ancestorentry= dn2ancestor(be,&dn_newdn,&ancestorsdn,&txn,&err);
CACHE_RETURN( &inst->inst_cache, &ancestorentry );
ldap_result_matcheddn= slapi_ch_strdup((char *) slapi_sdn_get_dn(&ancestorsdn));
ldap_result_code= LDAP_NO_SUCH_OBJECT;
LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: New superior "
"does not exist matched %s, newsuperior = %s\n",
ldap_result_matcheddn == NULL ? "NULL" :
ldap_result_matcheddn,
slapi_sdn_get_ndn(dn_newsuperiordn), 0 );
slapi_sdn_done(&ancestorsdn);
goto error_return;
}
}
}
else
{
ldap_result_code= plugin_call_acl_plugin (pb, newparententry->ep_entry, NULL, NULL, SLAPI_ACL_ADD, ACLPLUGIN_ACCESS_DEFAULT, &errbuf );
if ( ldap_result_code != LDAP_SUCCESS )
{
ldap_result_message= errbuf;
LDAPDebug( LDAP_DEBUG_TRACE, "No access to new superior.\n", 0, 0, 0 );
goto error_return;
}
}
/* Check that the target entry has a parent */
if ( parententry == NULL )
{
/* If the entry a suffix, and we're root, then it's OK that the parent doesn't exist */
if (!(slapi_be_issuffix(pb->pb_backend, sdn)) && !isroot)
{
/* Here means that we didn't find the parent */
ldap_result_matcheddn = "NULL";
ldap_result_code= LDAP_NO_SUCH_OBJECT;
LDAPDebug( LDAP_DEBUG_TRACE, "Parent does not exist matched %s, parentdn = %s\n",
ldap_result_matcheddn, slapi_sdn_get_ndn(&dn_parentdn), 0 );
goto error_return;
}
}
/* If it is a replicated Operation or "subtree-rename" is on,
* it's allowed to rename entries with children */
if ( !is_replicated_operation && !entryrdn_get_switch() &&
slapi_entry_has_children( e->ep_entry ))
{
ldap_result_code = LDAP_NOT_ALLOWED_ON_NONLEAF;
goto error_return;
}
/*
* JCM - All the child entries must be locked in the cache, so the size of
* subtree that can be renamed is limited by the cache size.
*/
/* Save away a copy of the entry, before modifications */
slapi_pblock_set( pb, SLAPI_ENTRY_PRE_OP, slapi_entry_dup( e->ep_entry ));
/* create a copy of the entry and apply the changes to it */
if ( (ec = backentry_dup( e )) == NULL )
{
ldap_result_code= LDAP_OPERATIONS_ERROR;
goto error_return;
}
/* JCMACL - Should be performed before the child check. */
/* JCMACL - Why is the check performed against the copy, rather than the existing entry? */
ldap_result_code = plugin_call_acl_plugin (pb, ec->ep_entry,
NULL /*attr*/, NULL /*value*/, SLAPI_ACL_WRITE,
ACLPLUGIN_ACCESS_MODRDN, &errbuf );
if ( ldap_result_code != LDAP_SUCCESS )
{
goto error_return;
}
/* Set the new dn to the copy of the entry */
slapi_entry_set_sdn( ec->ep_entry, &dn_newdn );
if (entryrdn_get_switch()) { /* subtree-rename: on */
Slapi_RDN srdn;
/* Set the new rdn to the copy of the entry; store full dn in e_srdn */
slapi_rdn_init_all_sdn(&srdn, &dn_newdn);
slapi_entry_set_srdn(ec->ep_entry, &srdn);
slapi_rdn_done(&srdn);
}
/* create it in the cache - prevents others from creating it */
if (( cache_add_tentative( &inst->inst_cache, ec, NULL ) != 0 ) ) {
ec_in_cache = 0; /* not in cache */
/* allow modrdn even if the src dn and dest dn are identical */
if ( 0 != slapi_sdn_compare((const Slapi_DN *)&dn_newdn,
(const Slapi_DN *)sdn) ) {
/* somebody must've created it between dn2entry() and here */
/* JCMREPL - Hmm... we can't permit this to happen...? */
ldap_result_code= LDAP_ALREADY_EXISTS;
goto error_return;
}
/* so if the old dn is the same as the new dn, the entry will not be cached
until it is replaced with cache_replace */
} else {
ec_in_cache = 1;
}
/* Build the list of modifications required to the existing entry */
{
slapi_mods_init(&smods_generated,4);
slapi_mods_init(&smods_generated_wsi,4);
ldap_result_code = moddn_newrdn_mods(pb, slapi_sdn_get_ndn(sdn),
ec, &smods_generated_wsi, is_replicated_operation);
if (ldap_result_code != LDAP_SUCCESS) {
if (ldap_result_code == LDAP_UNWILLING_TO_PERFORM)
ldap_result_message = "Modification of old rdn attribute type not allowed.";
goto error_return;
}
if (!entryrdn_get_switch()) /* subtree-rename: off */
{
/*
* Remove the old entrydn index entry, and add the new one.
*/
slapi_mods_add( &smods_generated, LDAP_MOD_DELETE, LDBM_ENTRYDN_STR,
strlen(backentry_get_ndn(e)), backentry_get_ndn(e));
slapi_mods_add( &smods_generated, LDAP_MOD_REPLACE, LDBM_ENTRYDN_STR,
strlen(backentry_get_ndn(ec)), backentry_get_ndn(ec));
}
/*
* Update parentid if we have a new superior.
*/
if(slapi_sdn_get_dn(dn_newsuperiordn)!=NULL) {
char buf[40]; /* Enough for an ID */
if (parententry != NULL) {
sprintf( buf, "%lu", (u_long)parententry->ep_id );
slapi_mods_add_string(&smods_generated, LDAP_MOD_DELETE, LDBM_PARENTID_STR, buf);
}
if (newparententry != NULL) {
sprintf( buf, "%lu", (u_long)newparententry->ep_id );
slapi_mods_add_string(&smods_generated, LDAP_MOD_REPLACE, LDBM_PARENTID_STR, buf);
}
}
}
slapi_pblock_get( pb, SLAPI_MODIFY_MODS, &mods );
slapi_mods_init_byref(&smods_operation_wsi,mods);
/*
* We are about to pass the last abandon test, so from now on we are
* committed to finish this operation. Set status to "will complete"
* before we make our last abandon check to avoid race conditions in
* the code that processes abandon operations.
*/
if (operation) {
operation->o_status = SLAPI_OP_STATUS_WILL_COMPLETE;
}
if ( slapi_op_abandoned( pb ) ) {
goto error_return;
}
/*
* First, we apply the generated mods that do not involve any state information.
*/
if ( entry_apply_mods( ec->ep_entry, slapi_mods_get_ldapmods_byref(&smods_generated) ) != 0 )
{
ldap_result_code= LDAP_OPERATIONS_ERROR;
LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods failed for entry %s\n",
slapi_entry_get_dn_const(ec->ep_entry), 0, 0);
goto error_return;
}
/*
* Now we apply the generated mods that do involve state information.
*/
if (slapi_mods_get_num_mods(&smods_generated_wsi)>0)
{
if (entry_apply_mods_wsi(ec->ep_entry, &smods_generated_wsi, operation_get_csn(operation), is_replicated_operation)!=0)
{
ldap_result_code= LDAP_OPERATIONS_ERROR;
LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods_wsi failed for entry %s\n",
slapi_entry_get_dn_const(ec->ep_entry), 0, 0);
goto error_return;
}
}
/*
* Now we apply the operation mods that do involve state information.
* (Operational attributes).
* The following block looks redundent to the one above. But it may
* be necessary - check the comment for version 1.3.16.22.2.76 of
* this file and compare that version with its previous one.
*/
if (slapi_mods_get_num_mods(&smods_operation_wsi)>0)
{
if (entry_apply_mods_wsi(ec->ep_entry, &smods_operation_wsi, operation_get_csn(operation), is_replicated_operation)!=0)
{
ldap_result_code= LDAP_OPERATIONS_ERROR;
LDAPDebug( LDAP_DEBUG_TRACE, "ldbm_modrdn: entry_apply_mods_wsi (operational attributes) failed for entry %s\n",
slapi_entry_get_dn_const(ec->ep_entry), 0, 0);
goto error_return;
}
}
/* check that the entry still obeys the schema */
if ( slapi_entry_schema_check( pb, ec->ep_entry ) != 0 ) {
ldap_result_code = LDAP_OBJECT_CLASS_VIOLATION;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
goto error_return;
}
/* Check attribute syntax if any new values are being added for the new RDN */
if (slapi_mods_get_num_mods(&smods_operation_wsi)>0)
{
if (slapi_mods_syntax_check(pb, smods_generated_wsi.mods, 0) != 0)
{
ldap_result_code = LDAP_INVALID_SYNTAX;
slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &ldap_result_message);
goto error_return;
}
}
/*
* Update the DN CSN of the entry.
*/
entry_add_dncsn(ec->ep_entry,operation_get_csn(operation));
entry_add_rdn_csn(ec->ep_entry,operation_get_csn(operation));
/*
* If the entry has a new superior then the subordinate count
* of the parents must be updated.
*/
if(slapi_sdn_get_dn(dn_newsuperiordn)!=NULL)
{
/*
* Update the subordinate count of the parents to reflect the moved child.
*/
if ( parententry!=NULL )
{
retval = parent_update_on_childchange(&parent_modify_context,
PARENTUPDATE_DEL, NULL);
/* The parent modify context now contains info needed later */
if (0 != retval)
{
goto error_return;
}
}
if ( newparententry!=NULL )
{
retval = parent_update_on_childchange(&newparent_modify_context,
PARENTUPDATE_ADD, NULL);
/* The newparent modify context now contains info needed later */
if (0 != retval)
{
goto error_return;
}
}
}
/*
* If the entry has children then we're going to have to rename them all.
*/
if (slapi_entry_has_children( e->ep_entry ))
{
/* JCM - This is where the subtree lock will appear */
if (entryrdn_get_switch()) /* subtree-rename: on */
{
children = moddn_get_children(&txn, pb, be, e, sdn,
&child_entries, &child_dns);
}
else
{
children = moddn_get_children(&txn, pb, be, e, sdn,
&child_entries, NULL);
}
/* JCM - Shouldn't we perform an access control check on all the children. */
/* JCMREPL - But, the replication client has total rights over its subtree, so no access check needed. */
/* JCM - A subtree move could break ACIs, static groups, and dynamic groups. */
}
if (!is_ruv && !is_fixup_operation) {
ruv_c_init = ldbm_txn_ruv_modify_context( pb, &ruv_c );
if (-1 == ruv_c_init) {
LDAPDebug( LDAP_DEBUG_ANY,
"ldbm_back_modrdn: ldbm_txn_ruv_modify_context "
"failed to construct RUV modify context\n",
0, 0, 0);
ldap_result_code = LDAP_OPERATIONS_ERROR;
retval = 0;
goto error_return;
}
}
/*
* make copies of the originals, no need to copy the mods because
* we have already copied them
*/
if ( (original_entry = backentry_dup( ec )) == NULL ) {
ldap_result_code= LDAP_OPERATIONS_ERROR;
goto error_return;
}
slapi_pblock_get(pb, SLAPI_MODRDN_TARGET_ENTRY, &target_entry);
if ( (original_targetentry = slapi_entry_dup(target_entry)) == NULL ) {
ldap_result_code= LDAP_OPERATIONS_ERROR;
goto error_return;
}
slapi_pblock_get(pb, SLAPI_MODRDN_NEWRDN, &newrdn);
original_newrdn = slapi_ch_strdup(newrdn);
slapi_pblock_get(pb, SLAPI_MODRDN_NEWSUPERIOR_SDN, &dn_newsuperiordn);
orig_dn_newsuperiordn = slapi_sdn_dup(dn_newsuperiordn);
} /* if (0 == retry_count) just once */
/* call the transaction pre modrdn plugins just after creating the transaction */
if ((retval = plugin_call_plugins(pb, SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN))) {
LDAPDebug1Arg( LDAP_DEBUG_TRACE, "SLAPI_PLUGIN_BE_TXN_PRE_MODRDN_FN plugin "
"returned error code %d\n", retval );
if (!ldap_result_code) {
slapi_pblock_get(pb, SLAPI_RESULT_CODE, &ldap_result_code);
}
if (!opreturn) {
slapi_pblock_get(pb, SLAPI_PLUGIN_OPRETURN, &opreturn);
}
if (!opreturn) {
slapi_pblock_set( pb, SLAPI_PLUGIN_OPRETURN, ldap_result_code ? &ldap_result_code : &retval );
}
goto error_return;
}
/*
* Update the indexes for the entry.
*/
retval = modrdn_rename_entry_update_indexes(&txn, pb, li, e, &ec, &smods_generated, &smods_generated_wsi, &smods_operation_wsi, &e_in_cache, &ec_in_cache);
if (DB_LOCK_DEADLOCK == retval)
{
/* Retry txn */
continue;
}
if (retval != 0 )
{
LDAPDebug( LDAP_DEBUG_TRACE, "modrdn_rename_entry_update_indexes failed, err=%d %s\n",
retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
MOD_SET_ERROR(ldap_result_code, LDAP_OPERATIONS_ERROR, retry_count);
goto error_return;
}
/*
* add new name to index
*/
{
char **rdns;
int i;
if ( (rdns = slapi_ldap_explode_rdn( slapi_sdn_get_dn(&dn_newrdn), 0 )) != NULL )
{
for ( i = 0; rdns[i] != NULL; i++ )
{
char *type;
Slapi_Value *svp[2];
Slapi_Value sv;
memset(&sv,0,sizeof(Slapi_Value));
if ( slapi_rdn2typeval( rdns[i], &type, &sv.bv ) != 0 )
{
LDAPDebug( LDAP_DEBUG_ANY, "modrdn: rdn2typeval (%s) failed\n",
rdns[i], 0, 0 );
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
MOD_SET_ERROR(ldap_result_code,
LDAP_OPERATIONS_ERROR, retry_count);
goto error_return;
}
svp[0] = &sv;
svp[1] = NULL;
retval = index_addordel_values_sv( be, type, svp, NULL, ec->ep_id, BE_INDEX_ADD, &txn );
if (DB_LOCK_DEADLOCK == retval)
{
/* To retry txn, once break "for loop" */
break;
}
else if (retval != 0 )
{
LDAPDebug( LDAP_DEBUG_ANY, "modrdn: could not add new value to index, err=%d %s\n",
retval, (msg = dblayer_strerror( retval )) ? msg : "", 0 );
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
MOD_SET_ERROR(ldap_result_code,
LDAP_OPERATIONS_ERROR, retry_count);
goto error_return;
}
}
slapi_ldap_value_free( rdns );
if (DB_LOCK_DEADLOCK == retval)
{
/* Retry txn */
continue;
}
}
}
if (slapi_sdn_get_dn(dn_newsuperiordn)!=NULL)
{
/* Push out the db modifications from the parent entry */
retval = modify_update_all(be, pb, &parent_modify_context, &txn);
if (DB_LOCK_DEADLOCK == retval)
{
/* Retry txn */
continue;
}
else if (0 != retval)
{
LDAPDebug( LDAP_DEBUG_ANY, "modrdn: "
"could not update parent, err=%d %s\n", retval,
(msg = dblayer_strerror( retval )) ? msg : "", 0 );
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
MOD_SET_ERROR(ldap_result_code,
LDAP_OPERATIONS_ERROR, retry_count);
goto error_return;
}
/* Push out the db modifications from the new parent entry */
else /* retval == 0 */
{
retval = modify_update_all(be, pb, &newparent_modify_context, &txn);
if (DB_LOCK_DEADLOCK == retval)
{
/* Retry txn */
continue;
}
if (0 != retval)
{
LDAPDebug( LDAP_DEBUG_ANY, "modrdn: "
"could not update parent, err=%d %s\n", retval,
(msg = dblayer_strerror( retval )) ? msg : "",
0 );
if (LDBM_OS_ERR_IS_DISKFULL(retval)) disk_full = 1;
MOD_SET_ERROR(ldap_result_code,
LDAP_OPERATIONS_ERROR, retry_count);
goto error_return;
}
}
}
/*
* Update ancestorid index.
*/
if (slapi_sdn_get_dn(dn_newsuperiordn)!=NULL) {
retval = ldbm_ancestorid_move_subtree(be, sdn, &dn_newdn, e->ep_id, children, &txn);