1 /* modrdn.c - ldbm backend modrdn routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 * LDAP v3 newSuperior support. Add new rdn as an attribute.
10 * (Full support for v2 also used software/ideas contributed
11 * by Roy Hooper rhooper@cyberus.ca, thanks to him for his
14 * Copyright 1999, Juan C. Gomez, All rights reserved.
15 * This software is not subject to any license of Silicon Graphics
16 * Inc. or Purdue University.
18 * Redistribution and use in source and binary forms are permitted
19 * without restriction or fee of any kind as long as this notice
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldbm.h"
33 #include "proto-back-ldbm.h"
40 AttributeDescription *children = slap_schema.si_ad_children;
41 AttributeDescription *entry = slap_schema.si_ad_entry;
42 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
43 struct berval p_dn, p_ndn;
44 struct berval new_dn = { 0, NULL}, new_ndn = { 0, NULL };
47 /* LDAP v2 supporting correct attribute handling. */
48 LDAPRDN new_rdn = NULL;
49 LDAPRDN old_rdn = NULL;
51 #define CAN_ROLLBACK -1
52 #define MUST_DESTROY 1
53 int rc = CAN_ROLLBACK;
56 const char *text = NULL;
57 char textbuf[SLAP_TEXT_BUFLEN];
58 size_t textlen = sizeof textbuf;
59 /* Added to support newSuperior */
60 Entry *np = NULL; /* newSuperior Entry */
61 struct berval *np_ndn = NULL; /* newSuperior ndn */
62 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
63 /* Used to interface with ldbm_modify_internal() */
64 Modifications *mod = NULL; /* Used to delete old/add new rdn */
65 int manageDSAit = get_manageDSAit( op );
68 LDAP_LOG( BACK_LDBM, ENTRY,
69 "ldbm_back_modrdn: dn: %s newSuperior=%s\n",
70 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
71 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len ) ? op->oq_modrdn.rs_newSup->bv_val : "NULL",0 );
73 Debug( LDAP_DEBUG_TRACE,
74 "==>ldbm_back_modrdn: dn: %s newSuperior=%s\n",
75 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "NULL",
76 ( op->oq_modrdn.rs_newSup && op->oq_modrdn.rs_newSup->bv_len )
77 ? op->oq_modrdn.rs_newSup->bv_val : "NULL", 0 );
80 /* grab giant lock for writing */
81 ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
83 e = dn2entry_w( op->o_bd, &op->o_req_ndn, &matched );
85 /* get entry with writer lock */
86 #ifdef LDAP_SYNCREPL /* FIXME: dn2entry() should return non-glue entry */
87 if (( e == NULL ) || ( !manageDSAit && e && is_entry_glue( e ))) {
91 if ( matched != NULL ) {
92 rs->sr_matched = strdup( matched->e_dn );
93 rs->sr_ref = is_entry_referral( matched )
94 ? get_entry_referrals( op, matched )
96 cache_return_entry_r( &li->li_cache, matched );
99 BerVarray deref = op->o_bd->syncinfo ?
100 op->o_bd->syncinfo->provideruri_bv : default_referral;
102 BerVarray deref = default_referral;
104 rs->sr_ref = referral_rewrite( deref, NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
107 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
109 rs->sr_err = LDAP_REFERRAL;
110 send_ldap_result( op, rs );
112 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
113 free( (char *)rs->sr_matched );
122 /* check entry for "entry" acl */
123 if ( ! access_allowed( op, e,
124 entry, NULL, ACL_WRITE, NULL ) )
127 LDAP_LOG( BACK_LDBM, ERR,
128 "ldbm_back_modrdn: no write access to entry of (%s)\n",
129 op->o_req_dn.bv_val, 0, 0 );
131 Debug( LDAP_DEBUG_TRACE,
132 "<=- ldbm_back_modrdn: no write access to entry\n", 0,
136 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
137 "no write access to entry" );
142 if (!manageDSAit && is_entry_referral( e ) ) {
143 /* parent is a referral, don't allow add */
144 /* parent is an alias, don't allow add */
145 rs->sr_ref = get_entry_referrals( op, e );
148 LDAP_LOG( BACK_LDBM, INFO,
149 "ldbm_back_modrdn: entry %s is a referral\n", e->e_dn, 0, 0 );
151 Debug( LDAP_DEBUG_TRACE, "entry %s is referral\n", e->e_dn,
155 rs->sr_err = LDAP_REFERRAL;
156 rs->sr_matched = e->e_name.bv_val;
157 send_ldap_result( op, rs );
159 if ( rs->sr_ref ) ber_bvarray_free( rs->sr_ref );
163 if ( has_children( op->o_bd, e ) ) {
165 LDAP_LOG( BACK_LDBM, INFO,
166 "ldbm_back_modrdn: entry %s has children\n", e->e_dn, 0, 0 );
168 Debug( LDAP_DEBUG_TRACE, "entry %s has children\n", e->e_dn,
172 send_ldap_error( op, rs, LDAP_NOT_ALLOWED_ON_NONLEAF,
173 "subtree rename not supported" );
177 if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
178 p_ndn = slap_empty_bv ;
180 dnParent( &e->e_nname, &p_ndn );
183 if ( p_ndn.bv_len != 0 ) {
184 /* Make sure parent entry exist and we can write its
188 if( (p = dn2entry_w( op->o_bd, &p_ndn, NULL )) == NULL) {
190 LDAP_LOG( BACK_LDBM, INFO,
191 "ldbm_back_modrdn: parent of %s does not exist\n",
194 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
198 send_ldap_error( op, rs, LDAP_OTHER,
199 "parent entry does not exist" );
204 /* check parent for "children" acl */
205 if ( ! access_allowed( op, p,
206 children, NULL, ACL_WRITE, NULL ) )
209 LDAP_LOG( BACK_LDBM, INFO,
210 "ldbm_back_modrdn: no access to parent of (%s)\n",
213 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
217 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
223 LDAP_LOG( BACK_LDBM, DETAIL1,
224 "ldbm_back_modrdn: wr to children of entry %s OK\n",
225 p_ndn.bv_val, 0, 0 );
227 Debug( LDAP_DEBUG_TRACE,
228 "ldbm_back_modrdn: wr to children of entry %s OK\n",
229 p_ndn.bv_val, 0, 0 );
232 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
233 p_dn = slap_empty_bv;
235 dnParent( &e->e_name, &p_dn );
239 LDAP_LOG( BACK_LDBM, DETAIL1,
240 "ldbm_back_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
242 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: parent dn=%s\n",
247 /* no parent, must be root to modify rdn */
248 isroot = be_isroot( op->o_bd, &op->o_ndn );
250 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
252 p = (Entry *)&slap_entry_root;
254 can_access = access_allowed( op, p,
255 children, NULL, ACL_WRITE, NULL );
258 /* check parent for "children" acl */
259 if ( ! can_access ) {
261 LDAP_LOG( BACK_LDBM, ERR,
262 "ldbm_back_modrdn: no access to parent \"\"\n", 0,0,0 );
264 Debug( LDAP_DEBUG_TRACE,
265 "<=- ldbm_back_modrdn: no "
266 "access to parent\n", 0, 0, 0 );
269 send_ldap_error( op, rs,
270 LDAP_INSUFFICIENT_ACCESS,
277 LDAP_LOG( BACK_LDBM, ERR,
278 "ldbm_back_modrdn: (%s) has no parent & not a root.\n",
281 Debug( LDAP_DEBUG_TRACE,
282 "<=- ldbm_back_modrdn: no parent & "
283 "not root\n", 0, 0, 0);
286 send_ldap_error( op, rs,
287 LDAP_INSUFFICIENT_ACCESS,
294 LDAP_LOG( BACK_LDBM, INFO,
295 "ldbm_back_modrdn: (%s) no parent, locked root.\n", e->e_dn, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE,
298 "ldbm_back_modrdn: no parent, locked root\n",
303 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
305 if ( op->oq_modrdn.rs_newSup != NULL ) {
307 LDAP_LOG( BACK_LDBM, DETAIL1,
308 "ldbm_back_modrdn: new parent \"%s\" requested\n",
309 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "ldbm_back_modrdn: new parent \"%s\" requested...\n",
313 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
316 np_ndn = op->oq_modrdn.rs_nnewSup;
318 /* newSuperior == oldParent? */
319 if ( dn_match( &p_ndn, np_ndn ) ) {
321 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: "
322 "new parent\"%s\" seems to be the same as the "
323 "old parent \"%s\"\n", op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
325 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: "
326 "new parent\"%s\" seems to be the same as the "
327 "old parent \"%s\"\n",
328 op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
331 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
335 if ( op->oq_modrdn.rs_newSup != NULL ) {
336 /* newSuperior == entry being moved?, if so ==> ERROR */
337 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
339 if ( op->oq_modrdn.rs_nnewSup->bv_len ) {
340 if( (np = dn2entry_w( op->o_bd, np_ndn, NULL )) == NULL) {
342 LDAP_LOG( BACK_LDBM, ERR,
343 "ldbm_back_modrdn: newSup(ndn=%s) not found.\n",
344 np_ndn->bv_val, 0, 0 );
346 Debug( LDAP_DEBUG_TRACE,
347 "ldbm_back_modrdn: newSup(ndn=%s) not here!\n",
348 np_ndn->bv_val, 0, 0);
351 send_ldap_error( op, rs, LDAP_OTHER,
352 "newSuperior not found" );
357 LDAP_LOG( BACK_LDBM, DETAIL1,
358 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
359 (void *) np, np->e_id, 0 );
361 Debug( LDAP_DEBUG_TRACE,
362 "ldbm_back_modrdn: wr to new parent OK np=%p, id=%ld\n",
363 (void *) np, np->e_id, 0 );
366 /* check newSuperior for "children" acl */
367 if ( !access_allowed( op, np, children, NULL,
371 LDAP_LOG( BACK_LDBM, INFO,
372 "ldbm_back_modrdn: no wr to newSup children.\n", 0, 0, 0 );
374 Debug( LDAP_DEBUG_TRACE,
375 "ldbm_back_modrdn: no wr to newSup children\n",
379 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS, NULL );
383 if ( is_entry_alias( np ) ) {
384 /* parent is an alias, don't allow add */
386 LDAP_LOG( BACK_LDBM, INFO,
387 "ldbm_back_modrdn: entry (%s) is an alias.\n", np->e_dn,0,0);
389 Debug( LDAP_DEBUG_TRACE, "entry is alias\n", 0, 0, 0 );
393 send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
394 "newSuperior is an alias" );
399 if ( is_entry_referral( np ) ) {
400 /* parent is a referral, don't allow add */
402 LDAP_LOG( BACK_LDBM, INFO,
403 "ldbm_back_modrdn: entry (%s) is a referral\n",
406 Debug( LDAP_DEBUG_TRACE, "entry (%s) is referral\n",
410 send_ldap_error( op, rs, LDAP_OTHER,
411 "newSuperior is a referral" );
418 /* no parent, must be root to modify newSuperior */
419 if ( isroot == -1 ) {
420 isroot = be_isroot( op->o_bd, &op->o_ndn );
424 if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
426 np = (Entry *)&slap_entry_root;
428 can_access = access_allowed( op, np,
429 children, NULL, ACL_WRITE, NULL );
432 /* check parent for "children" acl */
433 if ( ! can_access ) {
435 LDAP_LOG( BACK_LDBM, ERR,
436 "ldbm_back_modrdn: no access "
437 "to new superior \"\"\n", 0, 0, 0 );
439 Debug( LDAP_DEBUG_TRACE,
440 "<=- ldbm_back_modrdn: no "
441 "access to new superior\n", 0, 0, 0 );
444 send_ldap_error( op, rs,
445 LDAP_INSUFFICIENT_ACCESS,
452 LDAP_LOG( BACK_LDBM, ERR,
453 "ldbm_back_modrdn: \"\" not allowed as new superior\n",
456 Debug( LDAP_DEBUG_TRACE,
457 "<=- ldbm_back_modrdn: \"\" "
458 "not allowed as new superior\n",
462 send_ldap_error( op, rs,
463 LDAP_INSUFFICIENT_ACCESS,
471 LDAP_LOG( BACK_LDBM, DETAIL1,
472 "ldbm_back_modrdn: wr to new parent's children OK.\n", 0, 0, 0 );
474 Debug( LDAP_DEBUG_TRACE,
475 "ldbm_back_modrdn: wr to new parent's children OK\n",
479 new_parent_dn = op->oq_modrdn.rs_newSup;
482 /* Build target dn and make sure target entry doesn't exist already. */
483 build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn );
484 dnNormalize( 0, NULL, NULL, &new_dn, &new_ndn, NULL );
487 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: new ndn=%s\n",
488 new_ndn.bv_val, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: new ndn=%s\n",
491 new_ndn.bv_val, 0, 0 );
494 /* check for abandon */
495 if ( op->o_abandon ) {
499 if ( ( rc_id = dn2id ( op->o_bd, &new_ndn, &id ) ) || id != NOID ) {
500 /* if (rc_id) something bad happened to ldbm cache */
501 rs->sr_err = rc_id ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
502 send_ldap_result( op, rs );
507 LDAP_LOG( BACK_LDBM, INFO, "ldbm_back_modrdn: new ndn (%s) does not exist\n",
508 new_ndn.bv_val, 0, 0 );
510 Debug( LDAP_DEBUG_TRACE,
511 "ldbm_back_modrdn: new ndn=%s does not exist\n",
512 new_ndn.bv_val, 0, 0 );
515 /* Get attribute type and attribute value of our new rdn, we will
516 * need to add that to our new entry
518 if ( ldap_bv2rdn( &op->oq_modrdn.rs_newrdn, &new_rdn, (char **)&rs->sr_text,
519 LDAP_DN_FORMAT_LDAP ) )
522 LDAP_LOG ( OPERATION, ERR,
523 "ldbm_back_modrdn: can't figure out "
524 "type(s)/values(s) of newrdn\n",
527 Debug( LDAP_DEBUG_TRACE,
528 "ldbm_back_modrdn: can't figure out "
529 "type(s)/values(s) of newrdn\n",
536 LDAP_LOG ( OPERATION, RESULTS,
537 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
538 "new_rdn_val=\"%s\"\n",
539 new_rdn[ 0 ]->la_attr.bv_val,
540 new_rdn[ 0 ]->la_value.bv_val, 0 );
542 Debug( LDAP_DEBUG_TRACE,
543 "ldbm_back_modrdn: new_rdn_type=\"%s\", "
544 "new_rdn_val=\"%s\"\n",
545 new_rdn[ 0 ]->la_attr.bv_val,
546 new_rdn[ 0 ]->la_value.bv_val, 0 );
549 if ( op->oq_modrdn.rs_deleteoldrdn ) {
550 if ( ldap_bv2rdn( &op->o_req_dn, &old_rdn, (char **)&rs->sr_text,
551 LDAP_DN_FORMAT_LDAP ) )
554 LDAP_LOG ( OPERATION, ERR,
555 "ldbm_back_modrdn: can't figure out "
556 "type(s)/values(s) of old_rdn\n",
559 Debug( LDAP_DEBUG_TRACE,
560 "ldbm_back_modrdn: can't figure out "
561 "the old_rdn type(s)/value(s)\n",
569 LDAP_LOG( BACK_LDBM, DETAIL1, "ldbm_back_modrdn: DN_X500\n", 0, 0, 0 );
571 Debug( LDAP_DEBUG_TRACE, "ldbm_back_modrdn: DN_X500\n",
575 if ( slap_modrdn2mods( op, rs, e, old_rdn, new_rdn, &mod ) != LDAP_SUCCESS ) {
580 /* check for abandon */
581 if ( op->o_abandon ) {
586 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
587 send_ldap_error( op, rs, LDAP_OTHER,
588 "DN index delete fail" );
592 (void) cache_delete_entry( &li->li_cache, e );
595 /* XXX: there is no going back! */
600 e->e_nname = new_ndn;
601 new_dn.bv_val = NULL;
602 new_ndn.bv_val = NULL;
604 /* NOTE: after this you must not free new_dn or new_ndn!
605 * They are used by cache.
609 if ( dn2id_add( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
610 send_ldap_error( op, rs, LDAP_OTHER,
611 "DN index add failed" );
615 /* modify memory copy of entry */
616 rc_id = ldbm_modify_internal( op, &mod[0], e,
617 &rs->sr_text, textbuf, textlen );
625 send_ldap_result( op, rs );
629 /* here we may try to delete the newly added dn */
630 if ( dn2id_delete( op->o_bd, &e->e_nname, e->e_id ) != 0 ) {
631 /* we already are in trouble ... */
637 (void) cache_update_entry( &li->li_cache, e );
640 if ( id2entry_add( op->o_bd, e ) != 0 ) {
641 send_ldap_error( op, rs, LDAP_OTHER,
642 "entry update failed" );
646 rs->sr_err = LDAP_SUCCESS;
647 send_ldap_result( op, rs );
649 cache_entry_commit( e );
652 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
653 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
655 /* LDAP v2 supporting correct attribute handling. */
656 if ( new_rdn != NULL ) {
657 ldap_rdnfree( new_rdn );
659 if ( old_rdn != NULL ) {
660 ldap_rdnfree( old_rdn );
664 for (; mod; mod = tmp ) {
665 if ( mod->sml_nvalues ) free( mod->sml_nvalues[0].bv_val );
671 /* LDAP v3 Support */
673 /* free new parent and writer lock */
674 cache_return_entry_w( &li->li_cache, np );
678 /* free parent and writer lock */
679 cache_return_entry_w( &li->li_cache, p );
682 /* free entry and writer lock */
683 cache_return_entry_w( &li->li_cache, e );
684 if ( rc == MUST_DESTROY ) {
685 /* if rc == MUST_DESTROY the entry is uncached
686 * and its private data is destroyed;
687 * the entry must be freed */
690 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);